# BCB实现 Hmac_sha1算法源码

1 篇文章 0 订阅

到处找不到合适于BCB用的Hmac_sha1算法，openssl也不好用，于是找了些资料，自已编写了一个实现的类。实现二进制、十六进制和Base64编码的Hmac_sha1输出。

<pre name="code" class="cpp">#define SHA1_LITTLE_ENDIAN

#define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
#define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
#define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
#define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
#define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }

#define SHABLK0(i) (m_block->l[i] = \
(ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))

#define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))

#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
//==================================================================================================
typedef union
{
char  c[64];
unsigned long l[16];
} SHA1_WORKSPACE_BLOCK;
//==================================================================================================
class CHMAC_SHA1
{
private:
unsigned long m_state[5];
unsigned long m_count[2];
unsigned long __reserved1[1];
char  m_buffer[64];
char  m_digest[20];
unsigned long __reserved2[3];
char * szReport ;
char * SHA1_Key ;
char * AppendBuf1 ;
char * AppendBuf2 ;
char  m_workspace[64];
SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above

void Reset();
void Update(char *data, unsigned long len);
void Transform(unsigned long *state, char *buffer);
void Final();

enum {
SHA1_DIGEST_LENGTH	= 20,
SHA1_BLOCK_SIZE		= 64,
HMAC_BUF_LEN		= 4096
} ;
public:

CHMAC_SHA1()
:szReport(new char[HMAC_BUF_LEN]),
AppendBuf1(new char[HMAC_BUF_LEN]),
AppendBuf2(new char[HMAC_BUF_LEN]),
SHA1_Key(new char[HMAC_BUF_LEN])
{
m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace;
Reset();
}
~CHMAC_SHA1()
{
delete[] szReport ;
delete[] AppendBuf1 ;
delete[] AppendBuf2 ;
delete[] SHA1_Key ;
Reset();
}
void BinHMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *sha1);//二进制编码
String HexHMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len);//十六进制字符串
String Bas64HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len);//Base64编码

String Base64Encode(char *s,int len);
String Base64Decode(String source);
};
//=====================================================================================
void CHMAC_SHA1::Reset()
{
m_state[0] = 0x67452301;
m_state[1] = 0xEFCDAB89;
m_state[3] = 0x10325476;
m_state[4] = 0xC3D2E1F0;
m_count[0] = 0;
m_count[1] = 0;
}
//---------------------------------------------------------------------------------------
void CHMAC_SHA1::Update(char *data, unsigned long len)
{
unsigned long i, j;
j=(m_count[0] >> 3) & 63;
if((m_count[0] += len << 3) < (len << 3)) m_count[1]++;
m_count[1] += (len >> 29);
if((j + len) > 63)
{
i = 64 - j;
memcpy(&m_buffer[j], data, i);
Transform(m_state, m_buffer);
for(; i + 63 < len; i += 64)
Transform(m_state, &data[i]);
j = 0;
}
else
i = 0;
memcpy(&m_buffer[j], &data[i], len - i);
}
//-------------------------------------------------------------------------------------
void CHMAC_SHA1::Transform(unsigned long *state, char *buffer)
{
unsigned long a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
memcpy(m_block, buffer, 64);
_R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);  // 4 rounds of 20 operations each. Loop unrolled.
_R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
_R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
_R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
_R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
_R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
_R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
_R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
_R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
_R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
_R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
_R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
_R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
_R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
_R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
_R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
_R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
_R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
_R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
_R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);

state[0] += a;// Add the working vars back into state
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
}
//----------------------------------------------------------------------------------------------
void CHMAC_SHA1::Final()
{
unsigned long i;
char  finalcount[8];
for(i = 0; i < 8; i++)
finalcount[i] = (char)((m_count[((i >= 4) ? 0 : 1)]	>> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
Update((char *)"\200", 1);
while ((m_count[0] & 504) != 448)
Update((char *)"\0", 1);
Update(finalcount, 8); // Cause a SHA1Transform()
for(i = 0; i < 20; i++)
m_digest[i] = (char )((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
}
//------------------------------------------------------------------------------------------------
void CHMAC_SHA1::BinHMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len, BYTE *digest)
{
memset(SHA1_Key, 0, SHA1_BLOCK_SIZE);

if (key_len > SHA1_BLOCK_SIZE)/* STEP 1 */
{
Reset();
Update((char *)key, key_len);
Final();
memcpy(SHA1_Key, m_digest, 20);
}
else
memcpy(SHA1_Key, key, key_len);

for (int i=0; i<sizeof(m_ipad); i++)	/* STEP 2 */

Reset(); /* STEP 4 */
Final();
memcpy(szReport, m_digest, 20);

for (int j=0; j<sizeof(m_opad); j++)/* STEP 5 */

Reset();/*STEP 7 */
Final();
memcpy(digest, m_digest, 20);
}
//-----------------------------------------------------------------------------------
String CHMAC_SHA1::HexHMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len)
{
String sha1;
unsigned char binsha1[20];
BinHMAC_SHA1(text,text_len,key,key_len,binsha1);
for(int i=0;i<20;i++)
sha1+=IntToHex(binsha1[i],2);
return sha1;
}
//-----------------------------------------------------------------------------------
String CHMAC_SHA1::Bas64HMAC_SHA1(BYTE *text, int text_len, BYTE *key, int key_len)
{
String sha1;
String StrSha1;
unsigned char binsha1[20];
BinHMAC_SHA1(text,text_len,key,key_len,binsha1);
return Base64Encode(binsha1,20);
}
//==================================================================================
String CHMAC_SHA1::Base64Encode(char *s,int len)
{
int m_len; //字符串长度
int i; //循环变量
int m_tmp; //临时变量
AnsiString m_64code; //储存Base64编码的字符串
unsigned char* m_s; //临时存储参数字符串

char m_64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//Base64字符表
m_len = len;//取得字符串长度
m_s = s;
m_64code="";//返回串置空

for(i=0;i<m_len-m_len%3;i+=3) //处理3的倍数以内的字符
{
m_tmp=m_s[i]/4;
m_64code+=m_64[m_tmp];
m_tmp=m_s[i]%4*16 + m_s[i+1]/16;
m_64code+=m_64[m_tmp];
m_tmp=m_s[i+1]%16*4 + m_s[i+2]/64;
m_64code+=m_64[m_tmp];
m_tmp=m_s[i+2]%64;
m_64code+=m_64[m_tmp];
}

if(m_len%3==2)//如果字符串的长度被3除余2 ,不足的位数补0，尾部补“=”
{
m_tmp=m_s[m_len-2]/4;
m_64code+=m_64[m_tmp];
m_tmp=m_s[m_len-2]%4*16+m_s[m_len-1]/16;
m_64code+=m_64[m_tmp];
m_tmp=m_s[m_len-1]%16*4;
m_64code+=m_64[m_tmp];
m_64code+='=';
}

if(m_len%3==1) //如果字符串的长度被3除余1 ，不足的位数补0，尾部补两个“=”
{
m_tmp=m_s[m_len-1]/4;
m_64code+=m_64[m_tmp];
m_tmp=m_s[m_len-1]%4*16;
m_64code+=m_64[m_tmp];
m_64code+="==";
}
return m_64code;
}



12-31
02-28 3999

05-31 1002
06-25 1万+
05-16 3273
05-22 5万+
04-04
12-21 1316
11-02 1067
07-31
01-05 1653
02-05
12-18 5万+
07-03 3万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

jone7319

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。