[英]Generate SHA256 in c++
I need to generate SHA256 of some data. 我需要生成一些数据的SHA256。 I found this example is a very good one.
我发现这个例子非常好。 Now my question is Can I generate a sha256 by using my own key.
现在我的问题是我可以使用自己的密钥生成sha256。
EDIT: 编辑:
First of all, sorry for wrong question. 首先,抱歉错误的问题。 I don't mean that to change the key used to generate SHA256.
我并不是说要更改用于生成SHA256的密钥。 I really need is that, to convert the following java code to c++
我真的需要的是,将以下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;
}
SHA-256 is a member of SHA-2 cryptographic hash functions family, which usually generates 256 bits or 32 bytes HASH Code from an input message. SHA-256是SHA-2加密散列函数系列的成员,它通常从输入消息生成256位或32字节HASH代码 。
It's not an encryption mechanism which implies that from the HASH Code , also known as message digest or simply the digest , you can not regenerate the message. 它不是一种加密机制,它意味着从HASH代码 (也称为消息摘要或简称摘要)中 ,您无法重新生成消息。
Hence there's need no key to use SHA-256
to generate message digest . 因此,不需要使用
SHA-256
生成消息摘要的 密钥 。
Moreover, hash functions considered practically impossible to invert, that is, to recreate the input data from its hash value (message digest) alone. 此外, 哈希函数被认为实际上不可能反转,即仅从其哈希值(消息摘要)重新创建输入数据。 So You can't "decrypt" a HASH message/message digest to its input message, means reversing is not possible for Hashing.
因此,您无法将HASH消息/消息摘要“解密”到其输入消息,这意味着Hashing无法进行反转。 For example,
例如,
SHA256(plainText) -> digest
then there is NO mechanism like inverseSHA256
which can do the following, 再有就是没有像机制
inverseSHA256
能做到以下几点,
inverseSHA256(digest) -> plainText
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.