Example usage for org.bouncycastle.util Arrays copyOf

List of usage examples for org.bouncycastle.util Arrays copyOf

Introduction

In this page you can find the example usage for org.bouncycastle.util Arrays copyOf.

Prototype

public static BigInteger[] copyOf(BigInteger[] original, int newLength) 

Source Link

Usage

From source file:com.bitsofproof.supernode.api.ExtendedKey.java

License:Apache License

public static ExtendedKey parse(String serialized) throws ValidationException {
    byte[] data = ByteUtils.fromBase58WithChecksum(serialized);
    if (data.length != 78) {
        throw new ValidationException("invalid extended key");
    }/*from   w  ww  .  j a  v a2s .  c om*/
    byte[] type = Arrays.copyOf(data, 4);
    boolean hasPrivate;
    if (Arrays.areEqual(type, xprv) || Arrays.areEqual(type, tprv)) {
        hasPrivate = true;
    } else if (Arrays.areEqual(type, xpub) || Arrays.areEqual(type, tpub)) {
        hasPrivate = false;
    } else {
        throw new ValidationException("invalid magic number for an extended key");
    }

    int depth = data[4] & 0xff;

    int parent = data[5] & 0xff;
    parent <<= 8;
    parent |= data[6] & 0xff;
    parent <<= 8;
    parent |= data[7] & 0xff;
    parent <<= 8;
    parent |= data[8] & 0xff;

    int sequence = data[9] & 0xff;
    sequence <<= 8;
    sequence |= data[10] & 0xff;
    sequence <<= 8;
    sequence |= data[11] & 0xff;
    sequence <<= 8;
    sequence |= data[12] & 0xff;

    byte[] chainCode = Arrays.copyOfRange(data, 13, 13 + 32);
    byte[] pubOrPriv = Arrays.copyOfRange(data, 13 + 32, data.length);
    Key key;
    if (hasPrivate) {
        key = new ECKeyPair(new BigInteger(1, pubOrPriv), true);
    } else {
        key = new ECPublicKey(pubOrPriv, true);
    }
    return new ExtendedKey(key, chainCode, depth, parent, sequence);
}

From source file:com.distrimind.util.io.RandomByteArrayOutputStream.java

License:Open Source License

public byte[] getBytes() {
    return Arrays.copyOf(bytes, length);
}

From source file:com.github.horrorho.inflatabledonkey.chunk.store.disk.DiskChunk.java

License:Open Source License

DiskChunk(byte[] checksum, Path file) {
    this.file = Objects.requireNonNull(file, "file");
    this.checksum = Arrays.copyOf(checksum, checksum.length);
}

From source file:com.github.horrorho.inflatabledonkey.chunk.store.disk.DiskChunk.java

License:Open Source License

@Override
public byte[] checksum() {
    return Arrays.copyOf(checksum, checksum.length);
}

From source file:com.github.horrorho.inflatabledonkey.crypto.AESGCM.java

License:Open Source License

/**
 * Returns decrypted data./*from w  w  w  . j av  a2s .c om*/
 *
 * @param key
 * @param nonce nonce/ IV
 * @param header
 * @param encryptedData
 * @param tag
 * @param optional optional AADBytes (post header)
 * @return decrypted data
 * @throws IllegalArgumentException on decryption exceptions
 * @throws NullPointerException on null arguments
 */
public static byte[] decrypt(byte[] key, byte[] nonce, byte[] header, byte[] encryptedData, byte[] tag,
        Optional<byte[]> optional) {

    try {
        GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
        AEADParameters parameters = new AEADParameters(new KeyParameter(key), tag.length * 8, nonce, header);
        cipher.init(false, parameters);

        if (optional.isPresent()) {
            byte[] aadBytes = optional.get();
            cipher.processAADBytes(aadBytes, 0, aadBytes.length);
        }

        byte[] out = new byte[cipher.getOutputSize(encryptedData.length + tag.length)];

        int pos = cipher.processBytes(encryptedData, 0, encryptedData.length, out, 0);
        pos += cipher.processBytes(tag, 0, tag.length, out, pos);
        pos += cipher.doFinal(out, pos);

        return Arrays.copyOf(out, pos);

    } catch (IllegalStateException | InvalidCipherTextException ex) {
        throw new IllegalStateException("GCM decrypt error", ex);
    }
}

From source file:com.github.horrorho.inflatabledonkey.crypto.Curve25519.java

License:Open Source License

static byte[] clampPrivateKey(byte[] privateKey) {
    // NB little endian!
    byte[] copy = Arrays.copyOf(privateKey, privateKey.length);
    copy[0] &= 0xF8;/*from  w w w  .  ja v  a2  s  .c  o  m*/
    copy[31] &= 0x7F;
    copy[31] |= 0x40;
    return copy;
}

From source file:com.github.horrorho.inflatabledonkey.crypto.GCMDataB.java

License:Open Source License

public static byte[] decrypt(byte[] key, byte[] data) {
    // TODO utilize GCMAES#decrypt method
    try {//w w w.  ja v a 2  s .c om
        if (data.length < NONCE_LENGTH + TAG_LENGTH) {
            throw new IllegalArgumentException("data packet too short");
        }

        int cipherTextLength = data.length - NONCE_LENGTH - TAG_LENGTH;

        byte[] nonce = Arrays.copyOf(data, NONCE_LENGTH);

        GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
        AEADParameters parameters = new AEADParameters(new KeyParameter(key), TAG_LENGTH * 8, nonce);
        cipher.init(false, parameters);

        byte[] out = new byte[cipher.getOutputSize(cipherTextLength + TAG_LENGTH)];

        int pos = cipher.processBytes(data, NONCE_LENGTH, data.length - NONCE_LENGTH, out, 0);
        pos += cipher.doFinal(out, pos);

        return Arrays.copyOf(out, pos);

    } catch (IllegalStateException | InvalidCipherTextException ex) {
        throw new IllegalArgumentException(ex);
    }
}

From source file:com.github.horrorho.inflatabledonkey.data.backup.Asset.java

License:Open Source License

public Optional<byte[]> fileChecksum() {
    return fileChecksum.map(bs -> Arrays.copyOf(bs, bs.length));
}

From source file:com.github.horrorho.inflatabledonkey.data.backup.Asset.java

License:Open Source License

public Optional<byte[]> fileSignature() {
    return fileSignature.map(bs -> Arrays.copyOf(bs, bs.length));
}

From source file:com.github.horrorho.inflatabledonkey.data.backup.Asset.java

License:Open Source License

public Optional<byte[]> keyEncryptionKey() {
    return keyEncryptionKey.map(bs -> Arrays.copyOf(bs, bs.length));
}