|  | 
				
					
	
		
		
		SHA256 算法c++11 实现
   1 #ifndef SHA256_HPP2 #define SHA256_HPP
 3
 4
 5 #include <vector>
 6 #include <array>
 7 #include <algorithm>
 8
 9
 10
 11 class Sha256{
 12 public:
 13     Sha256(): len{0}, buf{0}, bits{0},
 14         key {
 15              0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
 16              0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
 17              0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
 18              0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
 19              0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
 20              0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
 21              0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
 22              0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
 23              0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
 24              0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
 25              0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
 26              0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
 27              0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL },
 28         hash {0}
 29     {
 30     }
 31
 32
 33 public:
 34     void sha256(const std::vector<unsigned char> &data, std::array<unsigned char, 32>& out){
 35         init();
 36         update(data);
 37         done(out);
 38     }
 39
 40
 41 private:
 42     void init() {
 43         len     = 0;
 44         bits[0] = bits[1] = 0;
 45         hash[0] = 0x6a09e667;
 46         hash[1] = 0xbb67ae85;
 47         hash[2] = 0x3c6ef372;
 48         hash[3] = 0xa54ff53a;
 49         hash[4] = 0x510e527f;
 50         hash[5] = 0x9b05688c;
 51         hash[6] = 0x1f83d9ab;
 52         hash[7] = 0x5be0cd19;
 53     }
 54
 55     void _hash(const std::array<unsigned int, 64>& key){
 56         unsigned a, b, c, d, e, f, g, h, i;
 57         unsigned t[2];
 58         unsigned W[64];
 59
 60         a = hash[0];
 61         b = hash[1];
 62         c = hash[2];
 63         d = hash[3];
 64         e = hash[4];
 65         f = hash[5];
 66         g = hash[6];
 67         h = hash[7];
 68
 69         for (i = 0; i < 64; i++) {
 70             if ( i < 16 )
 71                 W[i] = _word(this->buf, _shw(i, 2));
 72             else
 73                 W[i] = _G1(W[i - 2]) + W[i - 7] + _G0(W[i - 15]) + W[i - 16];
 74
 75             t[0] = h + _S1(e) + _Ch(e, f, g) + key[i] + W[i];
 76             t[1] = _S0(a) + _Ma(a, b, c);
 77             h = g;
 78             g = f;
 79             f = e;
 80             e = d + t[0];
 81             d = c;
 82             c = b;
 83             b = a;
 84             a = t[0] + t[1];
 85         }
 86
 87         hash[0] += a;
 88         hash[1] += b;
 89         hash[2] += c;
 90         hash[3] += d;
 91         hash[4] += e;
 92         hash[5] += f;
 93         hash[6] += g;
 94         hash[7] += h;
 95     }
 96
 97     void update(const std::vector<unsigned char>& data){
 98         for(size_t i = 0; i < data.size(); i++){
 99             buf[len] = data[i];
 100             len++;
 101             if(len == buf.size()){
 102                 _hash(this->key);
 103                 _addbits(bits, buf.size() * 8);
 104                 len = 0;
 105             }
 106         }
 107     }
 108
 109     void done(std::array<unsigned char, 32>& out){
 110         unsigned int i, j;
 111         j = len % buf.size();
 112         buf[j] = 0x80;
 113         std::fill(buf.begin() + j + 1, buf.end(), 0x00);
 114
 115         if(len > 55){
 116             _hash(this->key);
 117             std::fill_n(buf.begin(), buf.size(), 0x00);
 118         }
 119
 120         _addbits(bits, len * 8);
 121         buf[63] = _shb(bits[0],  0);
 122         buf[62] = _shb(bits[0],  8);
 123         buf[61] = _shb(bits[0], 16);
 124         buf[60] = _shb(bits[0], 24);
 125         buf[59] = _shb(bits[1],  0);
 126         buf[58] = _shb(bits[1],  8);
 127         buf[57] = _shb(bits[1], 16);
 128         buf[56] = _shb(bits[1], 24);
 129         _hash(this->key);
 130
 131         for (i = 0, j = 24; i < 4; i++, j -= 8) {
 132             out[i     ] = _shb(hash[0], j);
 133             out[i +  4] = _shb(hash[1], j);
 134             out[i +  8] = _shb(hash[2], j);
 135             out[i + 12] = _shb(hash[3], j);
 136             out[i + 16] = _shb(hash[4], j);
 137             out[i + 20] = _shb(hash[5], j);
 138             out[i + 24] = _shb(hash[6], j);
 139             out[i + 28] = _shb(hash[7], j);
 140         }
 141     }
 142
 143 private:
 144
 145     /* -------------------------------------------------------------------------- */
 146     inline uint8_t _shb(uint32_t x, uint32_t n) {
 147         return ( (x >> (n & 31)) & 0xff );
 148     } /* _shb */
 149
 150     /* -------------------------------------------------------------------------- */
 151     inline uint32_t _shw(uint32_t x, uint32_t n) {
 152         return ( (x << (n & 31)) & 0xffffffff );
 153     } /* _shw */
 154
 155     /* -------------------------------------------------------------------------- */
 156     inline uint32_t _r(uint32_t x, uint8_t n) {
 157         return ( (x >> n) | _shw(x, 32 - n) );
 158     } /* _r */
 159
 160     /* -------------------------------------------------------------------------- */
 161     inline uint32_t _Ch(uint32_t x, uint32_t y, uint32_t z)
 162     {
 163         return ( (x & y) ^ ((~x) & z) );
 164     } /* _Ch */
 165
 166     /* -------------------------------------------------------------------------- */
 167     inline uint32_t _Ma(uint32_t x, uint32_t y, uint32_t z)
 168     {
 169         return ( (x & y) ^ (x & z) ^ (y & z) );
 170     } /* _Ma */
 171
 172     /* -------------------------------------------------------------------------- */
 173     inline uint32_t _S0(uint32_t x) {
 174         return ( _r(x, 2) ^ _r(x, 13) ^ _r(x, 22) );
 175     } /* _S0 */
 176
 177     /* -------------------------------------------------------------------------- */
 178     inline uint32_t _S1(uint32_t x) {
 179         return ( _r(x, 6) ^ _r(x, 11) ^ _r(x, 25) );
 180     } /* _S1 */
 181
 182     /* -------------------------------------------------------------------------- */
 183     inline uint32_t _G0(uint32_t x) {
 184         return ( _r(x, 7) ^ _r(x, 18) ^ (x >> 3) );
 185     } /* _G0 */
 186
 187     /* -------------------------------------------------------------------------- */
 188     uint32_t _G1(uint32_t x) {
 189         return ( _r(x, 17) ^ _r(x, 19) ^ (x >> 10) );
 190     } /* _G1 */
 191
 192     /* -------------------------------------------------------------------------- */
 193     uint32_t _word(std::array<unsigned char, 64>& data, const int & pos ) {
 194         return ( _shw(data[pos + 0], 24) | _shw(data[pos + 1], 16)
 195                 | _shw(data[ pos + 2], 8) | (data[pos + 3]) );
 196     } /* _word */
 197
 198     /* -------------------------------------------------------------------------- */
 199     void _addbits(std::array<unsigned int, 2>& bits, uint32_t n) {
 200         if ( bits[0] > (0xffffffff - n) )
 201             bits[1] = (bits[1] + 1) & 0xFFFFFFFF;
 202         bits[0] = (bits[0] + n) & 0xFFFFFFFF;
 203     } /* _addbits */
 204
 205
 206
 207 private:
 208     size_t len;
 209     std::array<unsigned char, 64> buf;
 210     std::array<unsigned int, 2> bits;
 211     const std::array<unsigned int, 64> key;
 212     std::array<unsigned int, 8> hash;
 213
 214 };
 215
 216 #endif
 217
 调用方式, QT为例
  QString qSource = ui->leSource->text(); 
  QByteArray qSourceBytes = qSource.toUtf8(); 
  std::vector<unsigned char> sourceBytes(qSourceBytes.size()); 
  std::copy_n(qSourceBytes.begin(), qSourceBytes.size(), sourceBytes.begin()); 
  
  Sha256 sha246; 
  std::array<unsigned char, 32> out; 
  sha246.sha256(sourceBytes, out); 
  
  QByteArray qSha256Bytes(out.size(), 0); 
  std::copy_n(out.begin(), out.size(), qSha256Bytes.begin()); 
  qDebug() << TIMESTAMP << "source:" << qSource << ", sha256 data:" << qSha256Bytes.toHex();   
	    
    
 |