public byte[] hmac(byte[] key, byte[] bytes) throws InvalidKeyException { mac.init(new SecretKeySpec(key, mac.getAlgorithm())); return mac.doFinal(bytes); }
public byte[] hi(byte[] str, byte[] salt, int iterations) throws InvalidKeyException { mac.init(new SecretKeySpec(str, mac.getAlgorithm())); mac.update(salt); byte[] u1 = mac.doFinal(new byte[]{0, 0, 0, 1}); byte[] prev = u1; byte[] result = u1; for (int i = 2; i <= iterations; i++) { byte[] ui = hmac(str, prev); result = xor(result, ui); prev = ui; } return result; }
public HmacDigest(String algorithm, String secretKey) { Assert.notNull(algorithm, "Null algorithm"); Assert.notNull(secretKey, "Null secretKey"); try { mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(secretKey.getBytes(utf8), mac.getAlgorithm())); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { throw new IllegalArgumentException(ex); } }
@Override public Hasher newHasher() { if (supportsClone) { try { return new MacHasher((Mac) prototype.clone()); } catch (CloneNotSupportedException e) { // falls through } } return new MacHasher(getMac(prototype.getAlgorithm(), key)); }
mac.init(new SecretKeySpec(key, 0, 20, mac.getAlgorithm()));
@Override public Hasher newHasher() { if (supportsClone) { try { return new MacHasher((Mac) prototype.clone()); } catch (CloneNotSupportedException e) { // falls through } } return new MacHasher(getMac(prototype.getAlgorithm(), key)); }
@Override public Hasher newHasher() { if (supportsClone) { try { return new MacHasher((Mac) prototype.clone()); } catch (CloneNotSupportedException e) { // falls through } } return new MacHasher(getMac(prototype.getAlgorithm(), key)); }
@Override public Hasher newHasher() { if (supportsClone) { try { return new MacHasher((Mac) prototype.clone()); } catch (CloneNotSupportedException e) { // falls through } } return new MacHasher(getMac(prototype.getAlgorithm(), key)); }
mac.init(new SecretKeySpec(password.getDigest(), mac.getAlgorithm())); byte[] serverKey = mac.doFinal(ScramUtil.SERVER_KEY_BYTES); if(trace) saslScram.tracef("[C] Server key: %s%n", ByteIterator.ofBytes(serverKey).hexEncode().drainToString()); mac.init(new SecretKeySpec(serverKey, mac.getAlgorithm())); byte[] clientFirstMessage = finalResponse.getInitialResponse().getRawMessageBytes(); int bareStart = finalResponse.getInitialResponse().getInitialPartIndex();
mac.reset(); byte[] saltedPassword = initialResult.getScramDigestPassword().getDigest(); mac.init(new SecretKeySpec(saltedPassword, mac.getAlgorithm())); mac.update(ScramUtil.CLIENT_KEY_BYTES); clientKey = mac.doFinal(); mac.init(new SecretKeySpec(storedKey, mac.getAlgorithm())); final byte[] clientFirstMessage = clientMessage.getInitialResponse().getRawMessageBytes(); final int clientFirstMessageBareStart = clientMessage.getInitialResponse().getInitialPartIndex(); mac.init(new SecretKeySpec(saltedPassword, mac.getAlgorithm())); mac.update(ScramUtil.SERVER_KEY_BYTES); serverKey = mac.doFinal(); mac.init(new SecretKeySpec(serverKey, mac.getAlgorithm())); mac.update(clientFirstMessage, clientFirstMessageBareStart, clientFirstMessage.length - clientFirstMessageBareStart); mac.update((byte) ',');
public static byte[] sign(byte[] data, Algorithm algorithm, byte[] sharedSecret) { try { Mac mac = HMACProvider.getMAC(algorithm); mac.init(new SecretKeySpec(sharedSecret, mac.getAlgorithm())); mac.update(data); return mac.doFinal(); } catch (Exception e) { throw new RuntimeException(e); } }
final MessageDigest messageDigest = MessageDigest.getInstance(getMechanism().getMessageDigestName()); mac.init(new SecretKeySpec(saltedPassword, mac.getAlgorithm())); final byte[] clientKey = mac.doFinal(ScramUtil.CLIENT_KEY_BYTES); if(trace) saslScram.tracef("[C] Client key: %s", ByteIterator.ofBytes(clientKey).hexEncode().drainToString()); final byte[] storedKey = messageDigest.digest(clientKey); if(trace) saslScram.tracef("[C] Stored key: %s%n", ByteIterator.ofBytes(storedKey).hexEncode().drainToString()); mac.init(new SecretKeySpec(storedKey, mac.getAlgorithm())); final byte[] initialResponseBytes = initialResponse.getRawMessageBytes(); mac.update(initialResponseBytes, initialResponse.getInitialPartIndex(), initialResponseBytes.length - initialResponse.getInitialPartIndex());
/** * Method engineGetJCEAlgorithmString * {@inheritDoc} * */ protected String engineGetJCEAlgorithmString() { return this.macAlgorithm.getAlgorithm(); }
Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm()); mac.init(secret); byte[] digest = mac.doFinal(baseString.getBytes()); byte[] result=Base64.encode(digest, DEFAULT);
public JavaMac(String javaName, byte[] key) { try { mac = Mac.getInstance(javaName); mac.init(new SecretKeySpec(key, mac.getAlgorithm())); } catch (NoSuchAlgorithmException | InvalidKeyException ex) { throw new UnsupportedOperationException("Mac not supported: " + javaName, ex); } }
private static byte[] hmacSha1Hash(byte[] salt, String hostname) throws IOException { try { final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(salt, 0, salt.length, mac.getAlgorithm())); mac.update(hostname.getBytes()); return mac.doFinal(); } catch(GeneralSecurityException e) { throw new IOException(e); } }
public static byte[] sign(byte[] data, Algorithm algorithm, byte[] sharedSecret) { try { Mac mac = getMAC(algorithm); mac.init(new SecretKeySpec(sharedSecret, mac.getAlgorithm())); mac.update(data); return mac.doFinal(); } catch (Exception e) { throw new RuntimeException(e); } }
public static void initMacWithKey(Mac mac, Key key) throws org.jose4j.lang.InvalidKeyException { try { mac.init(key); } catch (InvalidKeyException e) { throw new org.jose4j.lang.InvalidKeyException("Key is not valid for " + mac.getAlgorithm(), e); } } }
public static String hmacSha256(String key, String data) throws AipException { try { Mac mac = Mac.getInstance("HmacSHA256"); SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), mac.getAlgorithm()); mac.init(signingKey); return encodeHex(mac.doFinal(data.getBytes())); } catch (Exception e) { e.printStackTrace(); throw new AipException(-1, "Fail to generate HMAC-SHA256 signature"); } }
public static byte[] computeHmac(byte[] key, Mac hmac, String data) { SecretKeySpec secretKey = new SecretKeySpec(key, hmac.getAlgorithm()); byte[] digest = computeHmac(secretKey, hmac, data); // Here we're finished with the SecretKey we created, so we can destroy it try { secretKey.destroy(); } catch (DestroyFailedException e) { // ignore } return digest; }