最近做一個關(guān)于網(wǎng)頁認證的測試程序,要用到sha-256算法,因為本人比較懶,在國內(nèi)論壇著了半天沒有發(fā)現(xiàn)可以直接拿來用的。最后在github上下載了一個,直接能用,共享給大家參考:
sha256.h
-
/*
-
* SHA-256 implementation.
-
*
-
* Copyright (c) 2010 Ilya O. Levin, http://www.literatecode.com
-
*
-
* Permission to use, copy, modify, and distribute this software for any
-
* purpose with or without fee is hereby granted, provided that the above
-
* copyright notice and this permission notice appear in all copies.
-
*
-
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
*/
-
#ifdef _MSC_VER
-
#ifndef uint8_t
-
typedef unsigned __int8 uint8_t;
-
#endif
-
#ifndef uint32_t
-
typedef unsigned __int32 uint32_t;
-
#endif
-
#ifndef uint64_t
-
typedef __int64 int64_t;
-
typedef unsigned __int64 uint64_t;
-
#endif
-
#else
-
#include <stdint.h>
-
#endif
-
-
#ifdef __cplusplus
-
extern "C"
-
{
-
#endif
-
-
typedef struct {
-
uint32_t buf[16];
-
uint32_t hash[8];
-
uint32_t len[2];
-
} sha256_context;
-
-
void sha256_init(sha256_context *);
-
void sha256_hash(sha256_context *, uint8_t * /* data */, uint32_t /* len */);
-
void sha256_done(sha256_context *, uint8_t * /* hash */);
-
-
#ifdef __cplusplus
-
}
-
#endif
sha256.c
-
/*
-
* SHA-256 implementation.
-
*
-
* Copyright (c) 2010 Ilya O. Levin, http://www.literatecode.com
-
*
-
* Permission to use, copy, modify, and distribute this software for any
-
* purpose with or without fee is hereby granted, provided that the above
-
* copyright notice and this permission notice appear in all copies.
-
*
-
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
*/
-
#define SWAP_BYTES
-
// #define USE_STD_MEMCPY
-
#define SELF_TEST
-
-
#ifdef USE_STD_MEMCPY
-
#include <string.h>
-
#endif
-
#include "sha256.h"
-
-
#ifdef __cplusplus
-
extern "C" {
-
#endif
-
-
#define RL(x,n) (((x) << n) | ((x) >> (32 - n)))
-
#define RR(x,n) (((x) >> n) | ((x) << (32 - n)))
-
-
#define S0(x) (RR((x), 2) ^ RR((x),13) ^ RR((x),22))
-
#define S1(x) (RR((x), 6) ^ RR((x),11) ^ RR((x),25))
-
#define G0(x) (RR((x), 7) ^ RR((x),18) ^ ((x) >> 3))
-
#define G1(x) (RR((x),17) ^ RR((x),19) ^ ((x) >> 10))
-
-
#ifdef SWAP_BYTES
-
#define BSWP(x,y) _bswapw((uint32_t *)(x), (uint32_t)(y))
-
#else
-
#define BSWP(p,n)
-
#endif
-
#ifdef USE_STD_MEMCPY
-
#define MEMCP(x,y,z) memcpy((x),(y),(z))
-
#else
-
#define MEMCP(x,y,z) _memcp((x),(y),(z))
-
#endif
-
-
#ifndef __cdecl
-
#define __cdecl
-
#endif
-
-
static const uint32_t K[64] = {
-
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
-
0x3956c25b, 0x59f111f1, 0x923
