本文整理了Java中javax.crypto.Mac.doFinal()
方法的一些代码示例,展示了Mac.doFinal()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mac.doFinal()
方法的具体详情如下:
包路径:javax.crypto.Mac
类名称:Mac
方法名:doFinal
[英]Computes the digest of this MAC based on the data previously specified in #update calls.
This Mac instance is reverted to its initial state and can be used to start the next MAC computation with the same parameters or initialized with different parameters.
[中]根据之前在#更新调用中指定的数据计算此MAC的摘要。
此Mac实例还原为其初始状态,可用于使用相同参数启动下一个Mac计算,或使用不同参数初始化。
代码示例来源:origin: commons-codec/commons-codec
/**
* Returns the digest for the input data.
*
* @param valueToDigest the input to use
* @return the digest as a byte[]
* @since 1.11
*/
public byte[] hmac(final ByteBuffer valueToDigest) {
mac.update(valueToDigest);
return mac.doFinal();
}
代码示例来源:origin: apache/kafka
public byte[] hmac(byte[] key, byte[] bytes) throws InvalidKeyException {
mac.init(new SecretKeySpec(key, mac.getAlgorithm()));
return mac.doFinal(bytes);
}
代码示例来源:origin: rapidoid/rapidoid
public static byte[] hmac(byte[] data, byte[] secret, byte[] salt) throws Exception {
Mac m = Mac.getInstance(HMAC_SHA_256);
m.init(new SecretKeySpec(secret, HMAC_SHA_256));
return m.doFinal(data);
}
代码示例来源:origin: google/guava
public void testEmptyInputs() throws Exception {
String knownOutput = "8cbf764cbe2e4623d99a41354adfd390";
Mac mac = Mac.getInstance("HmacMD5");
mac.init(MD5_KEY);
assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).newHasher().hash().toString());
}
代码示例来源:origin: resteasy/Resteasy
public static byte[] sign(byte[] data, Algorithm algorithm, SecretKey key)
{
try
{
Mac mac = HMACProvider.getMAC(algorithm);
mac.init(key);
mac.update(data);
return mac.doFinal();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static boolean verify(JWSInput input, SecretKey key)
代码示例来源:origin: alibaba/canal
/**
* 使用 HMAC-SHA1 签名方法对对encryptText进行签名
*
* @param encryptText 被签名的字符串
* @param encryptKey 密钥
* @return
* @throws Exception
*/
private byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
byte[] data = encryptKey.getBytes(ENCODING);
// 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
// 生成一个指定 Mac 算法 的 Mac 对象
Mac mac = Mac.getInstance(MAC_NAME);
// 用给定密钥初始化 Mac 对象
mac.init(secretKey);
byte[] text = encryptText.getBytes(ENCODING);
// 完成 Mac 操作
return mac.doFinal(text);
}
代码示例来源:origin: signalapp/Signal-Server
private byte[] getHmac(byte[] key, byte[] input, Mac mac) {
try {
mac.init(new SecretKeySpec(key, "HmacSHA256"));
return mac.doFinal(input);
} catch (InvalidKeyException e) {
throw new AssertionError(e);
}
}
代码示例来源:origin: knowm/XChange
@Override
public String digestParams(RestInvocation restInvocation) {
Mac hmac512 = getMac();
// <method>:<PIN>:<tonce>
String message = this.method + ":" + this.pin + ":" + this.tonce;
hmac512.update(message.getBytes());
return String.format("%0128x", new BigInteger(1, hmac512.doFinal())).toLowerCase();
}
}
代码示例来源:origin: google/guava
public void testEmptyInputs_mixedAlgorithms() throws Exception {
String knownOutput = "8cbf764cbe2e4623d99a41354adfd390";
Mac mac = Mac.getInstance("HmacMD5");
mac.init(SHA1_KEY);
assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).newHasher().hash().toString());
}
代码示例来源:origin: prestodb/presto
private ByteString hmac(String algorithm, ByteString key) {
try {
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key.toByteArray(), algorithm));
return ByteString.of(mac.doFinal(data));
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
} catch (InvalidKeyException e) {
throw new IllegalArgumentException(e);
}
}
代码示例来源:origin: stackoverflow.com
public static String encode(String key, String data) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
}
public static void main(String [] args) throws Exception {
System.out.println(encode("key", "The quick brown fox jumps over the lazy dog"));
}
代码示例来源:origin: knowm/XChange
String digest(String httpMethod, String invocationUrl, String reqContentType, String now) {
Mac mac256 = getMac();
mac256.update(httpMethod.toLowerCase().getBytes());
mac256.update(invocationUrl.toLowerCase().getBytes());
if (!"GET".equals(httpMethod)) {
mac256.update(reqContentType.toLowerCase().getBytes());
}
mac256.update(now.toLowerCase().getBytes());
return Base64.getEncoder().encodeToString(mac256.doFinal());
}
}
代码示例来源:origin: spring-cloud/spring-cloud-config
private static byte[] hmacSha256(String data, byte[] key) throws Exception {
String algorithm = HMAC_SHA256;
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes(UTF8));
}
代码示例来源:origin: stackoverflow.com
SecretKeySpec keySpec = new SecretKeySpec(
"qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50".getBytes(),
"HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(keySpec);
byte[] result = mac.doFinal("foo".getBytes());
BASE64Encoder encoder = new BASE64Encoder();
System.out.println(encoder.encode(result));
代码示例来源:origin: knowm/XChange
@Override
public String digestParams(RestInvocation restInvocation) {
Mac mac256 = getMac();
mac256.update(restInvocation.getParamValue(FormParam.class, "nonce").toString().getBytes());
mac256.update(clientId.getBytes());
mac256.update(publicApiKey.getBytes());
return String.format("%064x", new BigInteger(1, mac256.doFinal())).toUpperCase();
}
}
代码示例来源:origin: hs-web/hsweb-framework
/**
* This method uses the JCE to provide the crypto algorithm.
* HMAC computes a Hashed Message Authentication Code with the
* crypto hash algorithm as a parameter.
*
* @param crypto: the crypto algorithm (HmacSHA1, HmacSHA256,
* HmacSHA512)
* @param keyBytes: the bytes to use for the HMAC key
* @param text: the message or text to be authenticated
*/
private static byte[] hmac_sha(String crypto, byte[] keyBytes,
byte[] text) {
try {
Mac hmac;
hmac = Mac.getInstance(crypto);
SecretKeySpec macKey =
new SecretKeySpec(keyBytes, "RAW");
hmac.init(macKey);
return hmac.doFinal(text);
} catch (GeneralSecurityException gse) {
throw new UndeclaredThrowableException(gse);
}
}
代码示例来源:origin: aws/aws-sdk-java
protected byte[] sign(byte[] data, byte[] key,
SigningAlgorithm algorithm) throws SdkClientException {
try {
Mac mac = algorithm.getMac();
mac.init(new SecretKeySpec(key, algorithm.toString()));
return mac.doFinal(data);
} catch (Exception e) {
throw new SdkClientException(
"Unable to calculate a request signature: "
+ e.getMessage(), e);
}
}
代码示例来源:origin: knowm/XChange
@Override
public String digestParams(RestInvocation restInvocation) {
Mac mac256 = getMac();
mac256.update(restInvocation.getParamValue(FormParam.class, "nonce").toString().getBytes());
mac256.update(clientId.getBytes());
mac256.update(apiKey.getBytes());
return String.format("%064x", new BigInteger(1, mac256.doFinal())).toUpperCase();
}
}
代码示例来源:origin: mpusher/mpush
public static String hmacSha1(String data, String encryptKey) {
final String HMAC_SHA1 = "HmacSHA1";
SecretKeySpec signingKey = new SecretKeySpec(encryptKey.getBytes(Constants.UTF_8), HMAC_SHA1);
try {
Mac mac = Mac.getInstance(HMAC_SHA1);
mac.init(signingKey);
mac.update(data.getBytes(Constants.UTF_8));
return toHex(mac.doFinal());
} catch (Exception e) {
return Strings.EMPTY;
}
}
代码示例来源:origin: wildfly/wildfly
public static byte[] computeHMAC(byte[] kc, int sequenceNumber, Mac mac, byte[] message, int offset, int len) throws SaslException {
SecretKeySpec ks = new SecretKeySpec(kc, HMAC_algorithm);
try {
mac.init(ks);
} catch (InvalidKeyException e) {
throw saslDigest.mechInvalidKeyForDigestHMAC().toSaslException();
}
byte[] buffer = new byte[len + 4];
integerByteOrdered(sequenceNumber, buffer, 0, 4);
System.arraycopy(message, offset, buffer, 4, len);
byte[] macBuffer = new byte[10];
System.arraycopy(mac.doFinal(buffer), 0, macBuffer, 0, 10);
return macBuffer;
}
内容来源于网络,如有侵权,请联系作者删除!