Refine search
public static byte[] hmacSha512(byte[] key, byte[] input) { HMac hMac = new HMac(new SHA512Digest()); hMac.init(new KeyParameter(key)); hMac.update(input, 0, input.length); byte[] out = new byte[64]; hMac.doFinal(out, 0); return out; }
/** * Generate a new instance of an TlsMac. * * @param digest The digest to use. * @param key_block A byte-array where the key for this mac is located. * @param offset The number of bytes to skip, before the key starts in the buffer. * @param len The length of the key. */ protected TlsMac(Digest digest, byte[] key_block, int offset, int len) { this.mac = new HMac(digest); KeyParameter param = new KeyParameter(key_block, offset, len); this.mac.init(param); this.seqNo = 0; }
public void init( String pwStr, int keySize, byte[] salt, byte[] pwVerification ) throws ZipException { byte[] pwBytes = pwStr.getBytes(); super.saltBytes = salt; PBEParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init( pwBytes, salt, ITERATION_COUNT ); cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT*2 + 16); byte[] keyBytes = ((KeyParameter)cipherParameters).getKey(); this.cryptoKeyBytes = new byte[ KEY_SIZE_BYTE ]; System.arraycopy( keyBytes, 0, cryptoKeyBytes, 0, KEY_SIZE_BYTE ); this.authenticationCodeBytes = new byte[ KEY_SIZE_BYTE ]; System.arraycopy( keyBytes, KEY_SIZE_BYTE, authenticationCodeBytes, 0, KEY_SIZE_BYTE ); // based on SALT + PASSWORD (password is probably correct) this.pwVerificationBytes = new byte[ 2 ]; System.arraycopy( keyBytes, KEY_SIZE_BYTE*2, this.pwVerificationBytes, 0, 2 ); if( !ByteArrayHelper.isEqual( this.pwVerificationBytes, pwVerification ) ) { throw new ZipException("wrong password - " + ByteArrayHelper.toString(this.pwVerificationBytes) + "/ " + ByteArrayHelper.toString(pwVerification)); } // create the first 16 bytes of the key sequence again (using pw+salt) generator.init( pwBytes, salt, ITERATION_COUNT ); cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT); // checksum added to the end of the encrypted data, update on each encryption call this.mac = new HMac( new SHA1Digest() ); mac.init( new KeyParameter(authenticationCodeBytes) ); this.aesCipher = new SICBlockCipher(new AESEngine()); this.blockSize = aesCipher.getBlockSize(); // incremented on each 16 byte block and used as encryption NONCE (ivBytes) nonce = 1; }
/** * Creates a HKDFBytesGenerator based on the given hash function. * * @param hash the digest to be used as the source of generatedBytes bytes */ public HKDFBytesGenerator(Digest hash) { this.hMacHash = new HMac(hash); this.hashLen = hash.getDigestSize(); }
/** {@inheritDoc} */ @Override public void signSymm(SecurityPolicy policy, byte[] key, byte[] input, int inputOffset, int verifyLen, byte[] output, int outputOffset) throws ServiceResultException { HMac hmac = createMac(policy.getSymmetricSignatureAlgorithm(), new KeyParameter(key)); hmac.update(input, inputOffset, verifyLen); hmac.doFinal(output, outputOffset); }
private byte[] hmac(final byte[] contentToSign, final HashAlgoType hashAlgo) { HMac hmac = new HMac(hashAlgo.createDigest()); hmac.update(contentToSign, 0, contentToSign.length); byte[] signature = new byte[hmac.getMacSize()]; hmac.doFinal(signature, 0); return signature; }
public void init(DerivationParameters param) { if (!(param instanceof HKDFParameters)) { throw new IllegalArgumentException( "HKDF parameters required for HKDFBytesGenerator"); } HKDFParameters params = (HKDFParameters)param; if (params.skipExtract()) { // use IKM directly as PRK hMacHash.init(new KeyParameter(params.getIKM())); } else { hMacHash.init(extract(params.getSalt(), params.getIKM())); } info = params.getInfo(); generatedBytes = 0; currentT = new byte[hashLen]; }
mac.update(macHeader, 0, macHeader.length); mac.update(message, offset, len); byte[] result = new byte[mac.getMacSize()]; mac.doFinal(result, 0); return result;
public static WrappedMac getMac(ProtocolVersion version, CipherSuite cipherSuite, byte[] key) throws NoSuchAlgorithmException { MacAlgorithm macAlg = AlgorithmResolver.getMacAlgorithm(version, cipherSuite); if (macAlg == MacAlgorithm.HMAC_GOSTR3411) { GOST3411Digest digest = new GOST3411Digest(); return new ContinuousMac(new HMac(digest), digest, new KeyParameter(key)); } else if (macAlg == MacAlgorithm.HMAC_GOSTR3411_2012_256) { GOST3411_2012_256Digest digest = new GOST3411_2012_256Digest(); return new ContinuousMac(new HMac(digest), digest, new KeyParameter(key)); } else if (macAlg == MacAlgorithm.IMIT_GOST28147) { ParametersWithSBox parameters = new ParametersWithSBox(new KeyParameter(key), GOSTUtils.getGostSBox(cipherSuite)); return new ContinuousMac(new GOST28147Mac(), parameters); } else if (macAlg.getJavaName() != null) { return new JavaMac(macAlg.getJavaName(), key); } else { throw new NoSuchAlgorithmException("Mac: " + macAlg + " is not supported!"); } }
Digest digest = new SHA256Digest(); HMac hmac = new HMac(digest); hmac.init(new KeyParameter(appKeyHere)); hmac.update(requestURI, 0, lenOfReqURI); byte[] resBuf = new byte[digest.getDigestSize()]; hmac.doFinal(resBuf, 0); String resStr = new String(Hex.encode(resBuf)); // Contains final usable value
/** 10 bytes */ public byte[] getFinalAuthentication() { // MAC / based on encIn + PASSWORD + SALT (encryption was successful) byte[] macBytes = new byte[ mac.getMacSize() ]; mac.doFinal( macBytes, 0 ); byte[] macBytes10 = new byte[10]; System.arraycopy( macBytes, 0, macBytes10, 0, 10 ); return macBytes10; }
/** * Base constructor. * * @param digest digest to build the HMAC on. */ public HMacDSAKCalculator(Digest digest) { this.hMac = new HMac(digest); this.V = new byte[hMac.getMacSize()]; this.K = new byte[hMac.getMacSize()]; }
@Override public void write(byte[] bytes, int off, int len) throws IOException { hmac.update(bytes, off, len); }
private HMac createMac(SecurityAlgorithm algorithm, KeyParameter param) throws ServiceResultException { HMac hmac = null; if (algorithm.equals(SecurityAlgorithm.HmacSha1)) { hmac = new HMac(new SHA1Digest()); } else if (algorithm.equals(SecurityAlgorithm.HmacSha256)) { hmac = new HMac(new SHA256Digest()); } else { throw new ServiceResultException( StatusCodes.Bad_SecurityPolicyRejected, "Unsupported symmetric signature algorithm: " + algorithm); } hmac.init(param); return hmac; }
/** * Performs the expand part of the key derivation function, using currentT * as input and output buffer. * * @throws DataLengthException if the total number of bytes generated is larger than the one * specified by RFC 5869 (255 * HashLen) */ private void expandNext() throws DataLengthException { int n = generatedBytes / hashLen + 1; if (n >= 256) { throw new DataLengthException( "HKDF cannot generate more than 255 blocks of HashLen size"); } // special case for T(0): T(0) is empty, so no update if (generatedBytes != 0) { hMacHash.update(currentT, 0, hashLen); } hMacHash.update(info, 0, info.length); hMacHash.update((byte)n); hMacHash.doFinal(currentT, 0); }
/** * @return The Keysize of the mac. */ protected int getSize() { return mac.getMacSize(); }
@Override public byte[] getSignature() { byte[] signature = new byte[outLen]; hmac.doFinal(signature, 0); return signature; }
/** * Generate a new instance of an TlsMac. * * @param digest The digest to use. * @param key_block A byte-array where the key for this mac is located. * @param offset The number of bytes to skip, before the key starts in the buffer. * @param len The length of the key. */ protected TlsMac(Digest digest, byte[] key_block, int offset, int len) { this.mac = new HMac(digest); KeyParameter param = new KeyParameter(key_block, offset, len); this.mac.init(param); this.seqNo = 0; }
public void init( String pwStr, int keySize, byte[] salt, byte[] pwVerification ) throws ZipException { byte[] pwBytes = pwStr.getBytes(); super.saltBytes = salt; PBEParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init( pwBytes, salt, ITERATION_COUNT ); cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT*2 + 16); byte[] keyBytes = ((KeyParameter)cipherParameters).getKey(); this.cryptoKeyBytes = new byte[ KEY_SIZE_BYTE ]; System.arraycopy( keyBytes, 0, cryptoKeyBytes, 0, KEY_SIZE_BYTE ); this.authenticationCodeBytes = new byte[ KEY_SIZE_BYTE ]; System.arraycopy( keyBytes, KEY_SIZE_BYTE, authenticationCodeBytes, 0, KEY_SIZE_BYTE ); // based on SALT + PASSWORD (password is probably correct) this.pwVerificationBytes = new byte[ 2 ]; System.arraycopy( keyBytes, KEY_SIZE_BYTE*2, this.pwVerificationBytes, 0, 2 ); if( !ByteArrayHelper.isEqual( this.pwVerificationBytes, pwVerification ) ) { throw new ZipException("wrong password - " + ByteArrayHelper.toString(this.pwVerificationBytes) + "/ " + ByteArrayHelper.toString(pwVerification)); } // create the first 16 bytes of the key sequence again (using pw+salt) generator.init( pwBytes, salt, ITERATION_COUNT ); cipherParameters = generator.generateDerivedParameters(KEY_SIZE_BIT); // checksum added to the end of the encrypted data, update on each encryption call this.mac = new HMac( new SHA1Digest() ); mac.init( new KeyParameter(authenticationCodeBytes) ); this.aesCipher = new SICBlockCipher(new AESEngine()); this.blockSize = aesCipher.getBlockSize(); // incremented on each 16 byte block and used as encryption NONCE (ivBytes) nonce = 1; }
public PKCS5S2ParametersGenerator(Digest digest) { hMac = new HMac(digest); state = new byte[hMac.getMacSize()]; }