关于sha1算法的几个问题。

sound_of_silence 2006-04-07 05:46:26
//sha1.h
#ifndef _SHA1_H_
#define _SHA1_H_

#include <stdint.h>
/*
* If you do not have the ISO standard stdint.h header file, then you
* must typdef the following:
* name meaning
* uint32_t unsigned 32 bit integer
* uint8_t unsigned 8 bit integer (i.e., unsigned char)
* int_least16_t integer of >= 16 bits
*
*/

#ifndef _SHA_enum_
#define _SHA_enum_
enum
{
shaSuccess = 0,
shaNull, /* Null pointer parameter */
shaInputTooLong, /* input data too long */
shaStateError /* called Input after Result */
};
#endif
#define SHA1HashSize 20

/*
* This structure will hold context information for the SHA-1
* hashing operation
*/
typedef struct SHA1Context
{
uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */

uint32_t Length_Low; //不知道这两个变量有什么用?
uint32_t Length_High;

int_least16_t Message_Block_Index;
uint8_t Message_Block[64]; /* 512-bit message blocks */

int Computed; /* Is the digest computed? */
int Corrupted; /* Is the message digest corrupted? */
} SHA1Context;


int SHA1Reset( SHA1Context *);
int SHA1Input( SHA1Context *,
const uint8_t *,
unsigned int);
int SHA1Result( SHA1Context *,
uint8_t Message_Digest[SHA1HashSize]);

#endif

//sha1.c

int SHA1Input( SHA1Context *context,
const uint8_t *message_array,
unsigned length)
{
if (!length)
{
return shaSuccess;
}

if (!context || !message_array)
{
return shaNull;
}

if (context->Computed)
{
context->Corrupted = shaStateError;

return shaStateError;
}

if (context->Corrupted)
{
return context->Corrupted;
}
while(length-- && !context->Corrupted)
{
context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
context->Length_Low += 8;
if (context->Length_Low == 0)
{
context->Length_High++;
if (context->Length_High == 0)
{
/* Message is too long */
context->Corrupted = 1;
}
}

if (context->Message_Block_Index == 64)
{
SHA1ProcessMessageBlock(context);
}

message_array++;
}

return shaSuccess;
}

while循环里谁能详细解释一下?
...全文
365 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdlyczl 2007-03-15
  • 打赏
  • 举报
回复
自己画图看,不难理解的
lstc 2006-08-25
  • 打赏
  • 举报
回复
建议你看看SHA1算法的理论

4,451

社区成员

发帖
与我相关
我的任务
社区描述
云计算 云安全相关讨论
社区管理员
  • 云安全社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧