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

📄 sha.h

📁 RSA算法的VC源码
💻 H
字号:
/* SHA-1                                */
/* Secure Hash Algorithm 1              */
/* ------------------------------------ */
/* Copyright (C) 1997 Siemens AG Munich */
/* Author: Harald Amschler HL CC AT1    */
/*  V1.00: 18.12.1997                   */  /* PC version */
/*  V1.01: 07.01.1998                   */  /* transfer to C51 */
/*  V1.02: 12.01.1998                   */  /* C51 debugging, chaining */
/*  V1.03: 13.01.1998                   */  /* application note */
#include "ctype.h"
//#pragma mode2  /* compile for 66CXX */


/* typedefs: */

typedef unsigned char byte;
typedef unsigned int word;
typedef unsigned long int lword;


/* defines: */

/* modes for sha1_calculateMessageDigest(): */
#define SHA1_INIT      0x01  /* initialize algorithm */
#define SHA1_CHAINING  0x02  /* return after each 64 bytes block */


/* global variables: */

 byte *message;   /* pointer to message */
lword msgpos;     /* position in message buffer */
lword msglen;     /* total message length in bytes */
lword msgsize;    /* message length in block buffer */
 byte padlen;     /* padding length */
 byte msgl[4];    /* message length info in padding block */
lword h[5];       /* message digest */

void sha1_init(byte *_message, lword _msglen);
 int sha1_getMessageByte(void);


/*
// -----------------
// Initialize SHA-1:
// -----------------
*/

void sha1_init(byte *_message, lword _msgsize)
{
  message = _message;
  msgsize = _msgsize;
  msglen = 0;
  msgpos = 0;
  padlen = 255;
}


/*
// -----------------------
// Prepare for next block:
// -----------------------
*/

void sha1_nextblock(lword _msgsize)
{
  msgsize = _msgsize;
  msgpos = 0;
  padlen = 255;
}


/*
// -----------------------------------------------
// Get next byte of the message including padding:
// -----------------------------------------------
*/

int sha1_getMessageByte(void)
{
   char l;
  lword i;

  /*
  // Return message:
  */
  if (msgpos<msgsize)
  {
    msglen++;                  /* count total message size */
    return message[msgpos++];
  }
  /*
  // Padding:
  */
  if (padlen==255)            /* calculate padding size */
  {
    i = msglen*8;             /* message length in bits */
    msgl[3] = (char)(i>>24);  /* prepare for padding */
    msgl[2] = (char)(i>>16);
    msgl[1] = (char)(i>>8);
    msgl[0] = (char)(i);
    l = (byte)msgpos & 63;    /* actual block position */
    if (l<=54)                /* padding fits into last block? */
      padlen = 64-l;
    else                      /* no, additional block necessary */
      padlen = 128-l;
    padlen--;
    return 0x80;
  }
  if (padlen>0)
  {
    if (padlen-->4)
      return 0x00;
    return msgl[padlen];  /* padlen counts here from 3 down to 0 */
  }
  return -1;
}


/*
// ----------------------------------
// Circular Left Shift / Rotate Left:
// ----------------------------------
*/

lword sha1_rl(lword x,byte n)
{
  return (x<<n) | (x>>(32-n));
}


/*
// -------------------------
// Calculate message Digest:
// -------------------------
*/

int sha1_calculateMessageDigest(byte mode)
{
  static lword w[16];
         lword kt;
         lword a,b,c,d,e,temp;
          byte s,t;
           int i;

  /*
  // initialization:
  */
  if ((mode & SHA1_INIT)!=0)
  {
    h[0] = 0x67452301;
    h[1] = 0xEFCDAB89;
    h[2] = 0x98BADCFE;
    h[3] = 0x10325476;
    h[4] = 0xC3D2E1F0;
  }
  /*
  // calculation:
  */
  do
  {
    /*
    // read next message block:
    */
    for (s = 0; s<64; s++)
    {
      if ((i = sha1_getMessageByte())<0) break;  /* end of message */
      /*
        Note:
        SHA-1 assumes big endian!
        Intel PC: little endian
        C51     : big endian
      */
	  ((byte *)w)[s^3] = i;                      /* enable this line for PC */
	  //((byte *)w)[s] = i;                        /* C51 code */
    }
    if (i<0)
    {
      if (s==0) break;  /* message ended (correctly) */
      return 0;         /* incorrect padding! */
    }
    /*
    // process message block:
    */
    a = h[0];
    b = h[1];
    c = h[2];
    d = h[3];
    e = h[4];
    for (t = 0; t<80; t++)
    {
      switch (t)  /* set constant kt */
      {
        case 0:
          kt = 0x5A827999;
          break;
        case 20:
          kt = 0x6ED9EBA1;
          break;
        case 40:
          kt = 0x8F1BBCDC;
          break;
        case 60:
          kt = 0xCA62C1D6;
          break;
      }
      s = t & 15;
      if (t>=16)
        w[s] = sha1_rl(w[(s+13) & 15] ^
                       w[(s+8) & 15] ^
                       w[(s+2) & 15] ^
                       w[s],1);
      /* calculate function ft(B,C,D): */
      if (t<20) temp = (b & c) | ((~b) & d); else
        if (t<40) temp = b ^ c ^ d; else
          if (t<60) temp = (b & c) | (b & d) | (c & d); else
            temp = b ^c ^d;
      temp += sha1_rl(a,5) + e + w[s] + kt;
      e = d;
      d = c;
      c = sha1_rl(b,30);
      b = a;
      a = temp;
    }
    /*
    // set h[0..4]:
    */
    h[0] += a;
    h[1] += b;
    h[2] += c;
    h[3] += d;
    h[4] += e;
  }
  while ((mode & SHA1_CHAINING)==0);  /* continue if chaining disabled */
  return 1;  /* success */
}


/*
// -------------
// Main program:
// -------------
*/
void tohex( char *dd,char *hh,int len)
{
	char h1,h2;
	int i;
	char s1,s2;

	for (i=0;i<len;i++){
		h1 = hh[2*i];
		h2 = hh[2*i+1];
		s1 = toupper(h1) - 0x30;
		if (s1 > 9) s1 -= 7;
		s2 = toupper(h2) - 0x30;
		if (s2 > 9) s2 -= 7;
		dd[i] = s1*16 + s2;
    }
	//dd[len]='\0';
}

⌨️ 快捷键说明

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