⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sha1.c

📁 标准的sha1算法
💻 C
字号:

//7.2. c file
/*
 *  sha1.c
 *
 *  Description:
 *      This file implements the Secure Hashing Algorithm 1 as
 *      defined in FIPS PUB 180-1 published April 17, 1995.
 *
 *      The SHA-1, produces a 160-bit message digest for a given
 *      data stream.  It should take about 2**n steps to find a
 *      message with the same digest as a given message and
 *      2**(n/2) to find any two messages with the same digest,
 *      when n is the digest size in bits.  Therefore, this
 *      algorithm can serve as a means of providing a
 *      "fingerprint" for a message.
 *
 */
#include <sysdef.h>
#include <string.h>
#include "..\stos\typedef.h"
#include "..\hwinterfaces\st2064.h"
#include "cryptmem.h"
#include "stsha1.h"
#include "sha1.h"

#define MESSAGE_INPUT   &gCryptoMem[0]
#define DIGEST_OUTPUT   &gCryptoMem[64]

unsigned long int gShaMsgLen;

void Sha1Starts(void) {
    gShaMsgLen = 0;
    ShaReset();
}

void Sha1Update(const BYTE *input, WORD length) {
    BYTE left, fill;

    if( ! length ) return;

    left = (BYTE)(gShaMsgLen & 0x3F);
    fill = 64 - left;

    gShaMsgLen += length;

    if( left && (length >= fill) ) {
        memcpy( (MESSAGE_INPUT + left), input, fill );
        ShaProcess();
        length -= fill;
        input  += fill;
        left = 0;
    }

    while( length >= 64 ) {
        memcpy( MESSAGE_INPUT, input, 64 );
        ShaProcess();
        length -= 64;
        input  += 64;
    }

    if( length ) {
        memcpy( (MESSAGE_INPUT + left), input, length );
    }

}

const BYTE Sha1Padding[64] =
  {
    0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };

void Sha1Finish(BYTE *result) {
    unsigned long int bitLength;
    BYTE last, padn;
    BYTE msgLen[8];


    last = (BYTE)(gShaMsgLen & 0x3F);
    padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );

    bitLength = gShaMsgLen << 3;                   /* Nr of bits */
    msgLen[0] = 0x00;
    msgLen[1] = 0x00;
    msgLen[2] = 0x00;
    msgLen[3] = 0x00;
    msgLen[4] = (BYTE)(bitLength >> 24);
    msgLen[5] = (BYTE)(bitLength >> 16);
    msgLen[6] = (BYTE)(bitLength >> 8);
    msgLen[7] = (BYTE)(bitLength & 0xFF);

    Sha1Update( Sha1Padding, padn );
    Sha1Update( msgLen, 8 );

    if ( NULL != result ) {
        memcpy( result, DIGEST_OUTPUT, 20);
    }
}



#if 0
BYTE Result[20];
const BYTE data1[] = "111111111122222222223333333333444444444455555555556666666";
const BYTE data2[] = "9999999999666666666666666666668888888888777777777722222";
void main(void) {

    ShaInit();
    Sha1Update(data1, 57);
    Sha1Update(data2, 55);
    Sha1Finish(Result);

    while(1);
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -