2024年4月29日发(作者:)

基于C/C++的SHA-1算法实现,可实现对字符串的SHA-1计算

在VC++ 6.0下编译通过

运行截图如下:

包含四个文件,其中为main()所在文件,其余三个文件皆为sha-1算法实现。备注:sha1.h,stdint.h,皆为引

用它人源程序或Openssl标准库,非本人原创。为本人修改程序

1、 sha1.h

#ifndef _SHA1_H_

#define _SHA1_H_

#include "stdint.h"

#ifndef _SHA_enum_

#define _SHA_enum_

enum

{

shaSuccess = 0,

shaNull, /* 空指示参量 */

shaInputTooLong, /* 输入数据太长提示 */

shaStateError /* called Input after Result --以输入结果命名之 */

};

#endif

#define SHA1HashSize 20

/*

* 以下这种结构将会控制上下文消息 for the SHA-1

* hashing operation

*/

typedef struct SHA1Context

{

uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */

uint32_t Length_Low; /* Message length in bits */

uint32_t Length_High; /* Message length in bits */

/* Index into message block array */

int_least16_t Message_Block_Index;

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

int Computed; /* Is the digest computed? */

1 / 14

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_H_*/

2、 stdint.h

/* ISO C9x 7.18 Integer types

* Based on ISO/IEC SC22/WG14 9899 Committee draft (SC22 N2794)

*

* THIS SOFTWARE IS NOT COPYRIGHTED

*

* Contributor: Danny Smith <>

*

* This source code is offered for use in the public domain. You may

* use, modify or distribute it freely.

*

* This code is distributed in the hope that it will be useful but

* WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY

* DISCLAIMED. This includes but is not limited to warranties of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

*

* Date: 2000-12-02

*/

#ifndef _STDINT_H

#define _STDINT_H

#define __need_wint_t

#define __need_wchar_t

#include

/* 7.18.1.1 Exact-width integer types */

typedef signed char int8_t;

typedef unsigned char uint8_t;

typedef short int16_t;

typedef unsigned short uint16_t;

typedef int int32_t;

2 / 14