我需要生成一些数据的SHA256.我发现这个例子非常好.现在我的问题是我可以使用自己的密钥生成sha256.
编辑:
首先,抱歉错误的问题.我并不是说要更改用于生成SHA256的密钥.我真的需要的是,将以下java代码转换为c ++
public static String calculateHMAC(String data, String key) throws Exception {
String result;
try {
// get an hmac_sha2 key from the raw key bytes
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA2_ALGORITHM);
// get an hmac_sha1 Mac instance and initialize with the signing key
Mac sha256_HMAC = Mac.getInstance(HMAC_SHA2_ALGORITHM);
sha256_HMAC.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = sha256_HMAC.doFinal(data.getBytes());
// base64-encode the hmac
StringBuilder sb = new StringBuilder();
char[] charArray = Base64.encode(rawHmac);
for ( char a : charArray){
sb.append(a);
}
result = sb.toString();
}
catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
SHA-256是SHA-2加密散列函数系列的成员,它通常从输入消息生成256位或32字节HASH代码.
它不是一种加密机制,它意味着从HASH代码(也称为消息摘要或简称摘要)中,您无法重新生成消息.
因此,不需要使用密钥SHA-256来生成消息摘要.
此外,哈希函数被认为实际上不可能反转,即仅从其哈希值(消息摘要)重新创建输入数据.因此,您无法将HASH消息/消息摘要"解密"到其输入消息,这意味着Hashing无法进行反转.例如,
SHA256(plainText) -> digest
Run Code Online (Sandbox Code Playgroud)
那么就没有机制inverseSHA256可以做到以下几点,
inverseSHA256(digest) -> plainText
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29580 次 |
| 最近记录: |