青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

路漫漫,長修遠(yuǎn),我們不能沒有錢
隨筆 - 173, 文章 - 0, 評論 - 257, 引用 - 0
數(shù)據(jù)加載中……

SHA256 算法c++11 實現(xiàn)

SHA256 算法c++11 實現(xiàn)
  1 #ifndef SHA256_HPP
  2 #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              0x428a2f98UL0x71374491UL0xb5c0fbcfUL0xe9b5dba5UL0x3956c25bUL,
 16              0x59f111f1UL0x923f82a4UL0xab1c5ed5UL0xd807aa98UL0x12835b01UL,
 17              0x243185beUL0x550c7dc3UL0x72be5d74UL0x80deb1feUL0x9bdc06a7UL,
 18              0xc19bf174UL0xe49b69c1UL0xefbe4786UL0x0fc19dc6UL0x240ca1ccUL,
 19              0x2de92c6fUL0x4a7484aaUL0x5cb0a9dcUL0x76f988daUL0x983e5152UL,
 20              0xa831c66dUL0xb00327c8UL0xbf597fc7UL0xc6e00bf3UL0xd5a79147UL,
 21              0x06ca6351UL0x14292967UL0x27b70a85UL0x2e1b2138UL0x4d2c6dfcUL,
 22              0x53380d13UL0x650a7354UL0x766a0abbUL0x81c2c92eUL0x92722c85UL,
 23              0xa2bfe8a1UL0xa81a664bUL0xc24b8b70UL0xc76c51a3UL0xd192e819UL,
 24              0xd6990624UL0xf40e3585UL0x106aa070UL0x19a4c116UL0x1e376c08UL,
 25              0x2748774cUL0x34b0bcb5UL0x391c0cb3UL0x4ed8aa4aUL0x5b9cca4fUL,
 26              0x682e6ff3UL0x748f82eeUL0x78a5636fUL0x84c87814UL0x8cc70208UL,
 27              0x90befffaUL0xa4506cebUL0xbef9a3f7UL0xc67178f2UL },
 28         hash {0}
 29     {
 30     }
 31 
 32 
 33 public:
 34     void sha256(const std::vector<unsigned char> &data, std::array<unsigned char32>& 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 int64>& 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 char32>& 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 char64>& 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 int2>& 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 char64> buf;
210     std::array<unsigned int2> bits;
211     const std::array<unsigned int64> key;
212     std::array<unsigned int8> hash;
213 
214 };
215 
216 #endif
217 



調(diào)用方式, 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 char32> 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();



posted on 2021-06-03 17:31 Khan 閱讀(13537) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            麻豆成人在线观看| 午夜日韩福利| 久久综合五月| 亚洲日本在线观看| 麻豆成人在线| 卡一卡二国产精品| 久久久蜜桃一区二区人| 久久综合伊人77777| 久久九九热re6这里有精品| 久久av二区| 久久久99久久精品女同性| 欧美一区二区国产| 蜜臀a∨国产成人精品| 久久先锋影音av| 国产亚洲精品aa午夜观看| 国产美女精品人人做人人爽| 国产精品久久婷婷六月丁香| 黄网站色欧美视频| 在线播放视频一区| 亚洲一区二区在线免费观看| 亚洲午夜小视频| 亚洲欧美日韩久久精品| 久久亚洲欧美| 免费亚洲视频| 国产精品无码永久免费888| 国产精品一区二区三区久久| 国产视频丨精品|在线观看| 亚洲精品1区2区| 亚洲精品国产视频| 久久精品观看| 一本色道久久精品| 欧美一区二区视频免费观看| 久久免费视频网站| 久久久久久久综合| 亚洲国产另类久久精品| 亚洲欧美在线免费| 老鸭窝91久久精品色噜噜导演| 亚洲国产精品小视频| 亚洲福利视频一区二区| 亚洲最新视频在线播放| 欧美一站二站| 欧美小视频在线| 激情欧美日韩| 亚洲高清在线播放| 久久久噜噜噜久久人人看| 欧美激情精品久久久久| 久久国产精彩视频| 欧美黄色小视频| 欧美日韩一级黄| 亚洲第一在线| 欧美一二三视频| 99精品欧美一区| 在线观看一区二区视频| 欧美成年人视频| 国产一二精品视频| 一本大道久久精品懂色aⅴ| 欧美激情第一页xxx| 亚洲欧美在线高清| 国产欧美日韩不卡免费| 在线一区二区视频| 欧美一区视频在线| 亚洲综合二区| 欧美日韩一二三四五区| 在线一区日本视频| 欧美国产免费| 欧美88av| 激情综合网址| 欧美一区二区三区的| 亚洲综合第一页| 欧美日韩综合在线| 亚洲欧美在线网| 一本色道久久综合亚洲精品不卡| 欧美日韩一区在线播放| 一区二区三区免费观看| 99国产精品99久久久久久| 欧美国产日韩精品| 亚洲国产女人aaa毛片在线| 亚洲国产高清高潮精品美女| 久久久噜噜噜久久中文字幕色伊伊| 国内一区二区在线视频观看| 久久久精品性| 欧美风情在线观看| 欧美片在线播放| 一本到高清视频免费精品| 一区二区三区蜜桃网| 欧美三级乱人伦电影| 午夜在线视频一区二区区别| 亚洲综合精品| 欧美视频一二三区| 久久久精彩视频| 亚洲欧美中文字幕| 红桃视频亚洲| 欧美激情第8页| 欧美日韩综合久久| 午夜精品久久久久久久久| 欧美国产日韩精品免费观看| 欧美视频在线看| 欧美一区综合| 欧美国产精品一区| 亚洲一级在线观看| 久久久国产精品亚洲一区 | 久久婷婷成人综合色| 免费在线欧美黄色| 亚洲精品日韩欧美| 欧美一二三区精品| 亚洲风情亚aⅴ在线发布| 国产视频亚洲精品| 欧美成人精品h版在线观看| 欧美r片在线| 久久久久久久久蜜桃| 亚洲国产经典视频| 久久蜜桃av一区精品变态类天堂| 久久综合中文| 一区二区三区四区五区精品视频| 欧美在线一级va免费观看| 亚洲国产精品成人精品| 亚洲欧美欧美一区二区三区| 国内精品伊人久久久久av一坑 | 欧美福利一区| 欧美日韩一级片在线观看| 欧美刺激性大交免费视频| 欧美午夜精品久久久久久人妖| 免费久久精品视频| 国产精品jizz在线观看美国| 日韩亚洲一区二区| 欧美一区亚洲二区| 宅男噜噜噜66一区二区| 欧美高清视频一二三区| 欧美一区永久视频免费观看| 国产精品成人免费视频| 免费看精品久久片| 一区二区三区在线视频免费观看| 99re这里只有精品6| 久久米奇亚洲| 久久精品一二三| 欧美日韩三区| 一区二区日本视频| 亚洲乱码国产乱码精品精| 欧美成人国产| 噜噜噜91成人网| 激情欧美一区二区| 欧美在线观看天堂一区二区三区| 欧美日韩一区二区欧美激情| 亚洲欧洲一区二区在线观看| 尤物精品在线| 免费在线日韩av| 久久中文精品| 最近中文字幕日韩精品| 久久精品免视看| 欧美aa国产视频| 精品69视频一区二区三区| 午夜视频一区| 久久国产一区二区| 国产亚洲视频在线| 西西裸体人体做爰大胆久久久| 一区二区三区精品| 欧美精品一区在线观看| 这里只有精品电影| 亚洲一区二区三区中文字幕在线| 欧美午夜视频网站| 一本大道av伊人久久综合| 性欧美暴力猛交69hd| 国产精品久久久一区二区| 欧美亚洲日本国产| 看欧美日韩国产| 久久激情一区| 欧美激情视频在线免费观看 欧美视频免费一 | 欧美成人免费网站| 久久综合给合| 亚洲精品影院在线观看| 欧美激情国产日韩精品一区18| 一区二区三区国产精华| 亚洲综合大片69999| 美日韩在线观看| 巨胸喷奶水www久久久免费动漫| 久久久女女女女999久久| 亚洲精品乱码久久久久| 欧美激情1区| 午夜久久黄色| 免费在线亚洲| 午夜精品剧场| 精品999网站| 欧美视频第二页| 欧美一区中文字幕| 久久人人精品| 亚洲网站在线| 韩国三级在线一区| 欧美四级在线观看| 欧美一区二区播放| 一区二区三区久久网| 久久久亚洲高清| 亚洲欧美变态国产另类| 国内精品视频久久| 国产女主播一区| 欧美www视频| 久久免费精品日本久久中文字幕| 亚洲美女毛片| 亚洲国产三级在线| 久久成人18免费观看| 亚洲素人在线|