List of usage examples for org.bouncycastle.crypto.digests SHA256Digest SHA256Digest
public SHA256Digest()
From source file:com.completetrsst.crypto.Crypto.java
License:Apache License
private static byte[] _cryptIES(byte[] input, Key recipient, boolean forEncryption) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException { IESCipher cipher = new IESCipher(new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA256Digest()), new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())))); cipher.engineInit(forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, recipient, new SecureRandom()); return cipher.engineDoFinal(input, 0, input.length); }
From source file:com.DSC.crypto.Cipher.java
License:Open Source License
/** * /*ww w . j av a 2s.com*/ * @param priKey * @param pubKey * @param passphrase * @param data * @return * @throws InvalidCipherTextException */ public static byte[] encryptKey(CipherParameters priKey, CipherParameters pubKey, String passphrase, byte[] data) throws InvalidCipherTextException { /* Initialize IESEngine in stream mode */ IESEngine engine = new IESEngine(new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest())); /* Set the IESEngine cipher parameters as the passphrase and passphrase reversed */ IESParameters param = new IESParameters(passphrase.getBytes(), new StringBuilder(passphrase).reverse().toString().getBytes(), engine.getMac().getMacSize() * 8); /* Initialize the engine and encrypt the key */ engine.init(true, priKey, pubKey, param); return engine.processBlock(data, 0, data.length); }
From source file:com.DSC.crypto.Cipher.java
License:Open Source License
/** * //from w w w . j a v a 2s .c o m * @param priKey * @param pubKey * @param passphrase * @param data * @return * @throws InvalidCipherTextException */ public static byte[] decryptKey(CipherParameters priKey, CipherParameters pubKey, String passphrase, byte[] data) throws InvalidCipherTextException { /* IESEngine in stream mode */ IESEngine engine = new IESEngine(new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest())); /* Set the IESEngine cipher parameters as the passphrase and passphrase reversed */ IESParameters param = new IESParameters(passphrase.getBytes(), new StringBuilder(passphrase).reverse().toString().getBytes(), engine.getMac().getMacSize() * 8); /* Initialize the engine and decrypt the key */ engine.init(false, priKey, pubKey, param); return engine.processBlock(data, 0, data.length); }
From source file:com.DSC.crypto.ECDSA.java
License:Open Source License
/** * /*from ww w .j a va 2 s . c om*/ * @param data * @return */ private static byte[] hash(byte[] data) { Digest digest = new SHA256Digest(); byte[] hash = new byte[digest.getDigestSize()]; digest.update(data, 0, data.length); digest.doFinal(hash, 0); digest.reset(); return hash; }
From source file:com.facebook.delegatedrecovery.CountersignedRecoveryToken.java
License:Open Source License
/** * Utility method to quickly get a hex-encoded SHA256 digest of the data field * of the countersigned token, which contains the original token. * /*from w w w . ja v a 2 s . co m*/ * @return hex encoded string of SHA256 digest */ public String getInnerTokenHash() { final SHA256Digest digest = new SHA256Digest(); final byte[] hash = new byte[digest.getByteLength()]; digest.update(data, 0, data.length); digest.doFinal(hash, 0); return DelegatedRecoveryUtils.encodeHex(hash); }
From source file:com.facebook.delegatedrecovery.DelegatedRecoveryUtils.java
License:Open Source License
/** * Simple utility method to return a hex-encoded string of the SHA256 digest * of a byte[]/*from w ww. j a va 2 s . com*/ * * @param bytes The bytes to encode * @return The hex encoded bytes in a String */ public static String sha256(final byte[] bytes) { // hash of the token to re-identify it later final SHA256Digest digest = new SHA256Digest(); final byte[] hash = new byte[digest.getByteLength()]; digest.update(bytes, 0, bytes.length); digest.doFinal(hash, 0); return DelegatedRecoveryUtils.encodeHex(hash); }
From source file:com.facebook.delegatedrecovery.RecoveryToken.java
License:Open Source License
private byte[] getSignature(final byte[] rawArray, final ECPrivateKey privateKey) throws IOException { if (this.signature != null) { throw new IllegalStateException("This token already has a signature."); }// w w w . j a va 2 s. c om final BigInteger privatePoint = privateKey.getS(); final SHA256Digest digest = new SHA256Digest(); final byte[] hash = new byte[digest.getByteLength()]; digest.update(rawArray, 0, rawArray.length); digest.doFinal(hash, 0); final ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); signer.init(true, new ECPrivateKeyParameters(privatePoint, DelegatedRecoveryUtils.P256_DOMAIN_PARAMS)); final BigInteger[] signature = signer.generateSignature(hash); final ByteArrayOutputStream s = new ByteArrayOutputStream(); final DERSequenceGenerator seq = new DERSequenceGenerator(s); seq.addObject(new ASN1Integer(signature[0])); seq.addObject(new ASN1Integer(signature[1])); seq.close(); return s.toByteArray(); }
From source file:com.github.horrorho.inflatabledonkey.chunk.engine.ChunkChecksums.java
License:Open Source License
public static byte[] hashType1(byte[] data) { Digest digest = new SHA256Digest(); byte[] hash = new byte[digest.getDigestSize()]; digest.reset();//from w w w. ja v a 2 s . c o m digest.update(data, 0, data.length); digest.doFinal(hash, 0); digest.update(hash, 0, hash.length); digest.doFinal(hash, 0); byte[] checksum = Arrays.copyOf(hash, 20); logger.debug("-- hashType1() - hash: 0x{}", Hex.toHexString(checksum)); return checksum; }
From source file:com.github.horrorho.inflatabledonkey.chunk.store.ChunkDigest.java
License:Open Source License
public ChunkDigest() { this.digest = new SHA256Digest(); }
From source file:com.github.horrorho.inflatabledonkey.cloud.escrow.EscrowOperationsRecover.java
License:Open Source License
static NSDictionary decrypt(BlobA6 blob, byte[] key) { logger.debug("-- decrypt() - response blob: {}", blob); byte[] pcsData = AESCBC.decryptAESCBC(key, blob.iv(), blob.data()); logger.debug("-- decrypt() - pcs data: 0x{}", Hex.toHexString(pcsData)); BlobA0 pcsBlob = new BlobA0(ByteBuffer.wrap(pcsData)); logger.debug("-- decrypt() - pcs blob: {}", pcsBlob); byte[] derivedKey = PBKDF2.generate(new SHA256Digest(), pcsBlob.dsid(), pcsBlob.salt(), pcsBlob.iterations(), 16 * 8); logger.debug("-- decrypt() - derived key: 0x{}", Hex.toHexString(derivedKey)); byte[] saltIV = Arrays.copyOf(pcsBlob.salt(), 0x10); logger.debug("-- decrypt() - salt/ iv: 0x{}", Hex.toHexString(saltIV)); byte[] dictionaryData = AESCBC.decryptAESCBC(derivedKey, saltIV, pcsBlob.data()); logger.debug("-- decrypt() - dictionary data: 0x{}", Hex.toHexString(dictionaryData)); NSDictionary dictionary = PListsLegacy.parseDictionary(dictionaryData); logger.debug("-- decrypt() - dictionary: {}", dictionary.toXMLPropertyList()); return dictionary; }