// Path: src/main/java/at/ac/ait/archistar/backendserver/fragments/Fragment.java
// public interface Fragment {
// 
//     public enum EncryptionScheme {
//         NONE,
//         SHAMIR
//     };
// 
//     /**
//      * @param data the data that should be stored within the fragment
//      * @return the stored data
//      */
//     byte[] setData(byte[] data);
// 
//     /**
//      * @return the data that we believe to be on the storage server
//      */
//     byte[] getData();
// 
//     /**
//      * @return is this fragment already stored/synced with the backend? storage
//      * server
//      */
//     boolean isSynchronized();
// 
//     /**
//      * @param value set the synchronized state (is the fragment in-sync with the
//      * storage server side representation) to true
//      * @return the new state
//      */
//     boolean setSynchronized(boolean value);
// 
//     /**
//      * @return fragments ID upon the server
//      */
//     String getFragmentId();
// 
//     /**
//      * @return returns the used storage server
//      */
//     StorageServer getStorageServer();
// 
//     /**
//      * sets the fragment id
//      *
//      * @param string the new fragment id
//      * @return the set fragment id
//      */
//     String setFragmentId(String string);
// 
//     /**
//      * which encryption scheme was used for this fragment
//      * @return the used encryption scheme
//      */
//     EncryptionScheme getEncryptionScheme();
// 
//     /**
//      * sets the to-be-used encryption scheme
//      * 
//      * @param scheme the scheme which will be used to encrypt the data
//      */
//     void setEncryptionScheme(EncryptionScheme scheme);
// }


import at.ac.ait.archistar.backendserver.fragments.Fragment;
import at.archistar.crypto.CryptoEngine;
import at.archistar.crypto.data.InvalidParametersException;
import at.archistar.crypto.data.Share;
import at.archistar.crypto.data.ShareFactory;
import at.archistar.crypto.secretsharing.ReconstructionException;
import at.archistar.crypto.secretsharing.WeakSecurityException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import static org.fest.assertions.api.Assertions.assertThat;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package at.ac.ait.archistar.engine.crypto;


/**
 *
 * @author andy
 */
public class ArchistarSMCIntegrator {
        /**
     * split and encrypt data into fragments
     *
     * @param data the original data
     * @param fragments collection of expected fragments, the cryptoengine
     * should fill the existing fragments with the encrypted data
     * @return set of fragments containing encrypted data
     */
    public static Set<Fragment> encrypt(CryptoEngine engine, byte[] data, Set<Fragment> fragments) {
        try {
            Share[] shares = engine.share(data);
            Fragment[] fs = fragments.toArray(new Fragment[fragments.size()]);
            assertThat(fs.length == shares.length);
            assertThat(fragments.size() == 4);

            for (int i = 0; i < fs.length; i++) {

                Fragment f = fs[i];

                byte[] binData = shares[i].serialize();
                assert (binData != null);
                assert (binData.length != 0);

                f.setData(binData);
                f.setEncryptionScheme(Fragment.EncryptionScheme.SHAMIR);
            }
        } catch (IOException e) {
// Path: src/main/java/at/ac/ait/archistar/backendserver/fragments/Fragment.java
// public interface Fragment {
// 
//     public enum EncryptionScheme {
//         NONE,
//         SHAMIR
//     };
// 
//     /**
//      * @param data the data that should be stored within the fragment
//      * @return the stored data
//      */
//     byte[] setData(byte[] data);
// 
//     /**
//      * @return the data that we believe to be on the storage server
//      */
//     byte[] getData();
// 
//     /**
//      * @return is this fragment already stored/synced with the backend? storage
//      * server
//      */
//     boolean isSynchronized();
// 
//     /**
//      * @param value set the synchronized state (is the fragment in-sync with the
//      * storage server side representation) to true
//      * @return the new state
//      */
//     boolean setSynchronized(boolean value);
// 
//     /**
//      * @return fragments ID upon the server
//      */
//     String getFragmentId();
// 
//     /**
//      * @return returns the used storage server
//      */
//     StorageServer getStorageServer();
// 
//     /**
//      * sets the fragment id
//      *
//      * @param string the new fragment id
//      * @return the set fragment id
//      */
//     String setFragmentId(String string);
// 
//     /**
//      * which encryption scheme was used for this fragment
//      * @return the used encryption scheme
//      */
//     EncryptionScheme getEncryptionScheme();
// 
//     /**
//      * sets the to-be-used encryption scheme
//      * 
//      * @param scheme the scheme which will be used to encrypt the data
//      */
//     void setEncryptionScheme(EncryptionScheme scheme);
// }


// Path: src/main/java/at/ac/ait/archistar/engine/crypto/ArchistarSMCIntegrator.java
import at.ac.ait.archistar.backendserver.fragments.Fragment;
import at.archistar.crypto.CryptoEngine;
import at.archistar.crypto.data.InvalidParametersException;
import at.archistar.crypto.data.Share;
import at.archistar.crypto.data.ShareFactory;
import at.archistar.crypto.secretsharing.ReconstructionException;
import at.archistar.crypto.secretsharing.WeakSecurityException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import static org.fest.assertions.api.Assertions.assertThat;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package at.ac.ait.archistar.engine.crypto;


/**
 *
 * @author andy
 */
public class ArchistarSMCIntegrator {
        /**
     * split and encrypt data into fragments
     *
     * @param data the original data
     * @param fragments collection of expected fragments, the cryptoengine
     * should fill the existing fragments with the encrypted data
     * @return set of fragments containing encrypted data
     */
    public static Set<Fragment> encrypt(CryptoEngine engine, byte[] data, Set<Fragment> fragments) {
        try {
            Share[] shares = engine.share(data);
            Fragment[] fs = fragments.toArray(new Fragment[fragments.size()]);
            assertThat(fs.length == shares.length);
            assertThat(fragments.size() == 4);

            for (int i = 0; i < fs.length; i++) {

                Fragment f = fs[i];

                byte[] binData = shares[i].serialize();
                assert (binData != null);
                assert (binData.length != 0);

                f.setData(binData);
                f.setEncryptionScheme(Fragment.EncryptionScheme.SHAMIR);
            }
        } catch (IOException e) {
            assert (false);