sha1sum命令_python sha256

sha1sum命令_python sha256此工具从openssl源码中抠出部分代码,实现了一个类sha256

sha1sum命令_python

此工具从openssl源码中抠出部分代码,实现了一个类sha256。代码是匆匆从openssl里面抠出来的,没有经过细心整理。

头文件:sha256.h

#pragma once 
#include <string> 

#define SHA_LONG unsigned int 
#define SHA_LBLOCK 16
//#define SHA_LAST_BLOCK (SHA_CBLOCK-8) 
#define SHA256_CBLOCK (SHA_LBLOCK*4) /* SHA-256 treats input data as a
                     * contiguous array of 32 bit
                     * wide big-endian values. */
#define SHA256_DIGEST_LENGTH 32 
#define DATA_ORDER_IS_BIG_ENDIAN 

typedef struct SHA256state_st
    {
    SHA_LONG h[8];
    SHA_LONG Nl,Nh;
    SHA_LONG data[SHA_LBLOCK];
    unsigned int num,md_len;
    } SHA256_CTX;

//替换定义于通用算法里面的那些宏
#define HASH_LONG SHA_LONG 
#define HASH_CTX SHA256_CTX
#define HASH_CBLOCK SHA256_CBLOCK 

#define MD32_REG_T long 
#define X(i) XX##i 

#define HASH_MAKE_STRING(c,s) do { \
    unsigned long ll;       \
    unsigned int  nn;       \
    for (nn=0;nn<SHA256_DIGEST_LENGTH/4;nn++)   \
        {   ll=(c)->h[nn]; (void)HOST_l2c(ll,(s));   }  \
    } while (0)  



//来源于md32_common.h的定义,用于大端对齐DATA_ORDER_IS_BIG_ENDIAN
#define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
             l|=(((unsigned long)(*((c)++)))<<16),        \
             l|=(((unsigned long)(*((c)++)))<< 8),        \
             l|=(((unsigned long)(*((c)++)))       ),       \
             l)  
#define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
             *((c)++)=(unsigned char)(((l)>>16)&0xff),    \
             *((c)++)=(unsigned char)(((l)>> 8)&0xff),    \
             *((c)++)=(unsigned char)(((l)    )&0xff),  \
             l)  

class sha256
{
public:
    sha256(void);
    ~sha256(void);

public:
    void sha(const std::string& src, std::string& out);
private:
    int init(SHA256_CTX *c);
    int update(SHA256_CTX *c, const void *data_, size_t len);
    int final(unsigned char *md, HASH_CTX *c);
    void sha256_block_data_order (SHA256_CTX *c, const void *p, size_t num);
};

源文件sha256.cpp

#include "StdAfx.h"
#include "sha256.h"
#include <sstream>
#include <iomanip>

sha256::sha256(void)
{
}

sha256::~sha256(void)
{
}

void sha256::sha(const std::string& src, std::string& out) {
    SHA256_CTX c;  
    unsigned char m[SHA256_DIGEST_LENGTH];  
    memset(m,0,sizeof(m)); 

    init(&c); 
    update(&c,src.c_str(),src.length());  
    final(m,&c);  
    //OPENSSL_cleanse(&c,sizeof(c));  
    std::stringstream  hex;
    char buf[3]= {
  
  
  0};
    for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
        memset(buf,0,sizeof(buf)); 
        sprintf_s(buf,sizeof(buf),"%02X",m[i]);
        hex<<buf;
    }
    out = hex.str();
}

int sha256::init(SHA256_CTX *c) { 
    memset (c,0,sizeof(*c));
    c->h[0]=0x6a09e667UL;   c->h[1]=0xbb67ae85UL;
    c->h[2]=0x3c6ef372UL;   c->h[3]=0xa54ff53aUL;
    c->h[4]=0x510e527fUL;   c->h[5]=0x9b05688cUL;
    c->h[6]=0x1f83d9abUL;   c->h[7]=0x5be0cd19UL;
    c->md_len=SHA256_DIGEST_LENGTH;
    return 1;
}

//通用的算法,定义于openssl/md32_common.h 的HASH_UPDATE
#define HASH_BLOCK_DATA_ORDER sha256_block_data_order
int sha256::update(SHA256_CTX *c, const void *data_, size_t len) {
    const unsigned char *data=

今天的文章sha1sum命令_python sha256分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/85209.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注