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.github.horrorho.inflatabledonkey.pcs.xzone.XZone.java

License:Open Source License

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

From source file:com.github.horrorho.inflatabledonkey.pcs.xzone.XZone.java

License:Open Source License

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

From source file:com.github.horrorho.inflatabledonkey.pcs.zone.ProtectionZone.java

License:Open Source License

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

From source file:com.github.horrorho.inflatabledonkey.pcs.zone.ProtectionZone.java

License:Open Source License

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

From source file:com.github.horrorho.inflatabledonkey.pcs.zone.PZKeyDerivationFunction.java

License:Open Source License

public PZKeyDerivationFunction(Supplier<Digest> digestSupplier, byte[] label, int keyLength) {
    this.digestSupplier = Objects.requireNonNull(digestSupplier, "digestSupplier");
    this.label = Arrays.copyOf(label, label.length);
    this.keyLength = keyLength;
}

From source file:com.github.horrorho.inflatabledonkey.pcs.zone.PZKeyUnwrap.java

License:Open Source License

public PZKeyUnwrap(byte[] label, int keyLength) {
    this.label = Arrays.copyOf(label, label.length);
    this.keyLength = keyLength;
}

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

License:Open Source License

byte[] clampPrivateKey(byte[] privateKey) {
    byte[] copy = Arrays.copyOf(privateKey, privateKey.length);
    copy[0] &= 0xF8;//from   w  w  w  .j  a  v  a2s  . c om
    copy[31] &= 0x7F;
    copy[31] |= 0x40;
    return copy;
}

From source file:com.joyent.manta.serialization.EncryptedMultipartUploaSerializationHelper.java

License:Open Source License

/**
 * Decrypts and deserializes the specified binary blob.
 *
 * @param serializedData data to decrypt and deserialize
 * @return an upload object//from   w w w .j  a  v  a2s .  com
 */
@SuppressWarnings("unchecked")
public EncryptedMultipartUpload<WRAPPED> deserialize(final byte[] serializedData) {
    final byte[] iv = Arrays.copyOf(serializedData, cipherDetails.getIVLengthInBytes());

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Reading IV: {}", Hex.toHexString(iv));
    }

    final Cipher cipher = cipherDetails.getCipher();
    try {
        cipher.init(Cipher.DECRYPT_MODE, secretKey, cipherDetails.getEncryptionParameterSpec(iv));
    } catch (GeneralSecurityException e) {
        String msg = String.format("Unable to initialize cipher [%s]", cipherDetails.getCipherId());
        throw new MantaClientEncryptionException(msg, e);
    }

    final byte[] cipherText;

    if (cipherDetails.isAEADCipher()) {
        cipherText = extractCipherText(serializedData, iv.length, null);
    } else {
        final byte[] hmacIdBytes = Arrays.copyOfRange(serializedData,
                serializedData.length - CIPHER_ID_SIZE_BYTES, serializedData.length);
        final String hmacId = new String(hmacIdBytes, StandardCharsets.US_ASCII).trim();

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Verifying checksum with [{}]", hmacId);
        }

        final Supplier<HMac> hmacSupplier = SupportedHmacsLookupMap.INSTANCE.get(hmacId);

        if (hmacSupplier == null) {
            String msg = String.format("Unknown HMAC: [%s]", hmacId);
            throw new MantaClientEncryptionException(msg);
        }

        final HMac hmac = hmacSupplier.get();
        final int hmacLength = hmac.getMacSize();

        cipherText = extractCipherText(serializedData, iv.length, hmacLength);

        hmac.update(iv, 0, iv.length);
        hmac.update(cipherText, 0, cipherText.length);
        final byte[] calculated = new byte[hmacLength];
        hmac.doFinal(calculated, 0);

        final byte[] expected = Arrays.copyOfRange(serializedData,
                serializedData.length - hmacLength - CIPHER_ID_SIZE_BYTES,
                serializedData.length - CIPHER_ID_SIZE_BYTES);

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Expected HMAC:   {}", Hex.toHexString(expected));
            LOGGER.debug("Calculated HMAC: {}", Hex.toHexString(calculated));
        }

        if (!Arrays.areEqual(calculated, expected)) {
            String msg = "Serialization data ciphertext failed " + "cryptographic authentication";
            MantaClientEncryptionCiphertextAuthenticationException e = new MantaClientEncryptionCiphertextAuthenticationException(
                    msg);
            e.setContextValue("expected", Hex.toHexString(expected));
            e.setContextValue("calculated", Hex.toHexString(calculated));
            throw e;
        }
    }

    final byte[] plaintext;
    try {
        plaintext = cipher.doFinal(cipherText);
    } catch (GeneralSecurityException e) {
        String msg = "Error decrypting serialized object data";
        throw new MantaClientEncryptionException(msg, e);
    }

    final EncryptedMultipartUpload<WRAPPED> upload;

    try (Input input = new Input(plaintext)) {
        final int serializationVersion = input.readVarInt(true);

        if (serializationVersion != ENCRYPTED_MULTIPART_UPLOAD_SERIALIZATION_VERSION) {
            LOGGER.warn("Deserialized version [%d] is different than serialization version [%d",
                    serializationVersion, ENCRYPTED_MULTIPART_UPLOAD_SERIALIZATION_VERSION);
        }

        upload = (EncryptedMultipartUpload<WRAPPED>) kryo.readClassAndObject(input);
    }

    return upload;
}

From source file:com.raphfrk.craftproxyclient.net.types.values.BulkData.java

License:Open Source License

public BulkData(byte[] chunkData, int[] chunkX, int[] chunkZ, boolean skylight, short[] bitmap, short[] add) {
    this.chunkData = ByteUtils.clone(chunkData);
    this.chunkX = Arrays.copyOf(chunkX, chunkX.length);
    this.chunkZ = Arrays.copyOf(chunkZ, chunkZ.length);
    this.skylight = skylight;
    this.bitmap = new short[bitmap.length];
    System.arraycopy(bitmap, 0, this.bitmap, 0, bitmap.length);
    this.add = new short[add.length];
    System.arraycopy(add, 0, this.add, 0, add.length);
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncodingService.java

License:Open Source License

/**
 *
 * @param b64data1/*from   w w w. jav a  2  s.  c  o  m*/
 * @param b64data2
 * @return
 */
public String binaryConcatBase64(String b64data1, String b64data2) {
    byte[] data1 = Base64.decodeBase64(b64data1);
    byte[] data2 = Base64.decodeBase64(b64data2);

    int totalSize = data1.length + data2.length;

    byte[] both = Arrays.copyOf(data1, totalSize);
    System.arraycopy(data2, 0, both, data1.length, data2.length);

    return Base64.encodeBase64String(both);
}