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; }
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
/** * 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; }
/** * 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; }
/** * 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; }
CipherParameters p = new KeyParameter(key.getBytes("UTF-8")); WhirlpoolDigest w = new WhirlpoolDigest(); HMac hm = new HMac(w); hm.init(p); hm.update(inbytes, 0, inbytes.length); byte[] result = new byte[hm.getMacSize()]; hm.doFinal(result, 0);
public static String signRequest(String uri, String secret) throws Exception { byte[] r = uri.getBytes("US-ASCII"); byte[] k = secret.getBytes("US-ASCII"); HMac hmac = new HMac(new RIPEMD160Digest()); hmac.init(new KeyParameter(k)); hmac.update(r, 0, r.length); byte[] out = new byte[hmac.getMacSize()]; hmac.doFinal(out, 0); return new String((new Hex()).encode(out), "US-ASCII"); }
private byte[] hmac(byte[] contentToSign, HashAlgo hashAlgo) { HMac hmac = new HMac(hashAlgo.createDigest()); hmac.init(new KeyParameter(signingKey.getEncoded())); hmac.update(contentToSign, 0, contentToSign.length); byte[] signature = new byte[hmac.getMacSize()]; hmac.doFinal(signature, 0); return signature; }
public static byte[] generateHmac(byte[] key, String msg) throws UnsupportedEncodingException { HMac hmac = new HMac(new SHA256Digest()); byte[] result = new byte[hmac.getMacSize()]; byte[] msgAry = msg.getBytes("UTF-8"); hmac.init(new KeyParameter(key)); hmac.reset(); hmac.update(msgAry, 0, msgAry.length); hmac.doFinal(result, 0); return result; }
public static byte[] generateHmac512(byte[] key, String msg) throws UnsupportedEncodingException { HMac hmac = new HMac(new SHA512Digest()); byte[] result = new byte[hmac.getMacSize()]; byte[] msgAry = msg.getBytes("UTF-8"); hmac.init(new KeyParameter(key)); hmac.reset(); hmac.update(msgAry, 0, msgAry.length); hmac.doFinal(result, 0); return result; }
public static byte[] hmac(byte[] key, byte[] srcData) { KeyParameter keyParameter = new KeyParameter(key); SM3Digest digest = new SM3Digest(); HMac mac = new HMac(digest); mac.init(keyParameter); mac.update(srcData, 0, srcData.length); byte[] result = new byte[mac.getMacSize()]; mac.doFinal(result, 0); return result; } }
@Override public byte[] hmacSha384(byte[] m, byte[] key) { validateHmacSha384(m, key); Digest digest = new SHA384Digest(); HMac hmac = new HMac(digest); hmac.init(new KeyParameter(key)); byte[] out = new byte[hmac.getMacSize()]; hmac.update(m, 0, m.length); hmac.doFinal(out, 0); return out; }
public static byte[] hmacSha512(byte[] keyBytes, byte[] text) { HMac hmac = new HMac(new SHA512Digest()); byte[] resBuf = new byte[hmac.getMacSize()]; CipherParameters pm = new KeyParameter(keyBytes); hmac.init(pm); hmac.update(text, 0, text.length); hmac.doFinal(resBuf, 0); return resBuf; }
public static byte[] hmacSha512(byte[] keyBytes, byte[] text) { HMac hmac = new HMac(new SHA512Digest()); byte[] resBuf = new byte[hmac.getMacSize()]; CipherParameters pm = new KeyParameter(keyBytes); hmac.init(pm); hmac.update(text, 0, text.length); hmac.doFinal(resBuf, 0); return resBuf; }
/** * Create a {@link SimulatedTlsSRPIdentityManager} that implements the algorithm from RFC 5054 2.5.1.3 * * @param group the {@link SRP6GroupParameters} defining the group that SRP is operating in * @param seedKey the secret "seed key" referred to in RFC 5054 2.5.1.3 * @return an instance of {@link SimulatedTlsSRPIdentityManager} */ public static SimulatedTlsSRPIdentityManager getRFC5054Default(SRP6GroupParameters group, byte[] seedKey) { SRP6VerifierGenerator verifierGenerator = new SRP6VerifierGenerator(); verifierGenerator.init(group, TlsUtils.createHash(HashAlgorithm.sha1)); HMac mac = new HMac(TlsUtils.createHash(HashAlgorithm.sha1)); mac.init(new KeyParameter(seedKey)); return new SimulatedTlsSRPIdentityManager(group, verifierGenerator, mac); }
/** * Create a {@link SimulatedTlsSRPIdentityManager} that implements the algorithm from RFC 5054 2.5.1.3 * * @param group the {@link SRP6GroupParameters} defining the group that SRP is operating in * @param seedKey the secret "seed key" referred to in RFC 5054 2.5.1.3 * @return an instance of {@link SimulatedTlsSRPIdentityManager} */ public static SimulatedTlsSRPIdentityManager getRFC5054Default(SRP6GroupParameters group, byte[] seedKey) { SRP6VerifierGenerator verifierGenerator = new SRP6VerifierGenerator(); verifierGenerator.init(group, TlsUtils.createHash(HashAlgorithm.sha1)); HMac mac = new HMac(TlsUtils.createHash(HashAlgorithm.sha1)); mac.init(new KeyParameter(seedKey)); return new SimulatedTlsSRPIdentityManager(group, verifierGenerator, mac); }
public static byte[] generateHmac(byte[] key, byte[] msg) throws UnsupportedEncodingException { HMac hmac = new HMac(new SHA256Digest()); byte[] result = new byte[hmac.getMacSize()]; hmac.init(new KeyParameter(key)); hmac.reset(); hmac.update(msg, 0, msg.length); hmac.doFinal(result, 0); return result; }
public HmacContentSigner(HashAlgoType hashAlgo, AlgorithmIdentifier algorithmIdentifier, SecretKey signingKey) throws XiSecurityException { this.algorithmIdentifier = ParamUtil.requireNonNull("algorithmIdentifier", algorithmIdentifier); try { this.encodedAlgorithmIdentifier = algorithmIdentifier.getEncoded(); } catch (IOException ex) { throw new XiSecurityException("could not encode AlgorithmIdentifier", ex); } ParamUtil.requireNonNull("signingKey", signingKey); if (hashAlgo == null) { hashAlgo = AlgorithmUtil.extractHashAlgoFromMacAlg(algorithmIdentifier); } this.hmac = new HMac(hashAlgo.createDigest()); byte[] keyBytes = signingKey.getEncoded(); this.hmac.init(new KeyParameter(keyBytes, 0, keyBytes.length)); this.outLen = hmac.getMacSize(); this.outputStream = new HmacOutputStream(); }
/** * Internal OTP generation method. * * @param key Per-user key. * @param count Counter moving factor. * * @return Integer OTP. */ protected int generateInternal(final byte[] key, final long count) { final HMac hmac = new HMac(getDigest()); final byte[] output = new byte[hmac.getMacSize()]; hmac.init(new KeyParameter(key)); hmac.update(ByteUtil.toBytes(count), 0, 8); hmac.doFinal(output, 0); return truncate(output) % MODULUS[numberOfDigits]; }
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; }