Mercurial > vba-linux
view src/win32/7zip/7z/CPP/7zip/Crypto/Pbkdf2HmacSha1.cpp @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line source
1 // Pbkdf2HmacSha1.cpp3 #include "StdAfx.h"5 #include "HmacSha1.h"7 namespace NCrypto {8 namespace NSha1 {10 void Pbkdf2Hmac(const Byte *pwd, size_t pwdSize, const Byte *salt, size_t saltSize,11 UInt32 numIterations, Byte *key, size_t keySize)12 {13 CHmac baseCtx;14 baseCtx.SetKey(pwd, pwdSize);15 for (UInt32 i = 1; keySize > 0; i++)16 {17 CHmac ctx = baseCtx;18 ctx.Update(salt, saltSize);19 Byte u[kDigestSize] = { (Byte)(i >> 24), (Byte)(i >> 16), (Byte)(i >> 8), (Byte)(i) };20 const unsigned int curSize = (keySize < kDigestSize) ? (unsigned int)keySize : kDigestSize;21 ctx.Update(u, 4);22 ctx.Final(u, kDigestSize);24 unsigned int s;25 for (s = 0; s < curSize; s++)26 key[s] = u[s];28 for (UInt32 j = numIterations; j > 1; j--)29 {30 ctx = baseCtx;31 ctx.Update(u, kDigestSize);32 ctx.Final(u, kDigestSize);33 for (s = 0; s < curSize; s++)34 key[s] ^= u[s];35 }37 key += curSize;38 keySize -= curSize;39 }40 }42 void Pbkdf2Hmac32(const Byte *pwd, size_t pwdSize, const UInt32 *salt, size_t saltSize,43 UInt32 numIterations, UInt32 *key, size_t keySize)44 {45 CHmac32 baseCtx;46 baseCtx.SetKey(pwd, pwdSize);47 for (UInt32 i = 1; keySize > 0; i++)48 {49 CHmac32 ctx = baseCtx;50 ctx.Update(salt, saltSize);51 UInt32 u[kDigestSizeInWords] = { i };52 const unsigned int curSize = (keySize < kDigestSizeInWords) ? (unsigned int)keySize : kDigestSizeInWords;53 ctx.Update(u, 1);54 ctx.Final(u, kDigestSizeInWords);56 // Speed-optimized code start57 ctx = baseCtx;58 ctx.GetLoopXorDigest(u, numIterations - 1);59 // Speed-optimized code end61 unsigned int s;62 for (s = 0; s < curSize; s++)63 key[s] = u[s];65 /*66 // Default code start67 for (UInt32 j = numIterations; j > 1; j--)68 {69 ctx = baseCtx;70 ctx.Update(u, kDigestSizeInWords);71 ctx.Final(u, kDigestSizeInWords);72 for (s = 0; s < curSize; s++)73 key[s] ^= u[s];74 }75 // Default code end76 */78 key += curSize;79 keySize -= curSize;80 }81 }83 }}