mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
Add HMAC-SHA512 to hash
This commit is contained in:
41
src/hash.cpp
41
src/hash.cpp
@@ -56,3 +56,44 @@ unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char
|
||||
|
||||
return h1;
|
||||
}
|
||||
|
||||
int HMAC_SHA512_Init(HMAC_SHA512_CTX *pctx, const void *pkey, size_t len)
|
||||
{
|
||||
unsigned char key[128];
|
||||
if (len <= 128)
|
||||
{
|
||||
memcpy(key, pkey, len);
|
||||
memset(key + len, 0, 128-len);
|
||||
}
|
||||
else
|
||||
{
|
||||
SHA512_CTX ctxKey;
|
||||
SHA512_Init(&ctxKey);
|
||||
SHA512_Update(&ctxKey, pkey, len);
|
||||
SHA512_Final(key, &ctxKey);
|
||||
memset(key + 64, 0, 64);
|
||||
}
|
||||
|
||||
for (int n=0; n<128; n++)
|
||||
key[n] ^= 0x5c;
|
||||
SHA512_Init(&pctx->ctxOuter);
|
||||
SHA512_Update(&pctx->ctxOuter, key, 128);
|
||||
|
||||
for (int n=0; n<128; n++)
|
||||
key[n] ^= 0x5c ^ 0x36;
|
||||
SHA512_Init(&pctx->ctxInner);
|
||||
return SHA512_Update(&pctx->ctxInner, key, 128);
|
||||
}
|
||||
|
||||
int HMAC_SHA512_Update(HMAC_SHA512_CTX *pctx, const void *pdata, size_t len)
|
||||
{
|
||||
return SHA512_Update(&pctx->ctxInner, pdata, len);
|
||||
}
|
||||
|
||||
int HMAC_SHA512_Final(unsigned char *pmd, HMAC_SHA512_CTX *pctx)
|
||||
{
|
||||
unsigned char buf[64];
|
||||
SHA512_Final(buf, &pctx->ctxInner);
|
||||
SHA512_Update(&pctx->ctxOuter, buf, 64);
|
||||
return SHA512_Final(pmd, &pctx->ctxOuter);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user