github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-amcl/amcl/HASH256.go (about) 1 /* 2 Licensed to the Apache Software Foundation (ASF) under one 3 or more contributor license agreements. See the NOTICE file 4 distributed with this work for additional information 5 regarding copyright ownership. The ASF licenses this file 6 to you under the Apache License, Version 2.0 (the 7 "License"); you may not use this file except in compliance 8 with the License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, 13 software distributed under the License is distributed on an 14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 KIND, either express or implied. See the License for the 16 specific language governing permissions and limitations 17 under the License. 18 */ 19 20 /* 21 * Implementation of the Secure Hashing Algorithm (SHA-256) 22 * 23 * Generates a 256 bit message digest. It should be impossible to come 24 * come up with two messages that hash to the same value ("collision free"). 25 * 26 * For use with byte-oriented messages only. 27 */ 28 29 30 package amcl 31 32 33 const SHA256 int=32 34 35 const hash256_H0 uint32=0x6A09E667 36 const hash256_H1 uint32=0xBB67AE85 37 const hash256_H2 uint32=0x3C6EF372 38 const hash256_H3 uint32=0xA54FF53A 39 const hash256_H4 uint32=0x510E527F 40 const hash256_H5 uint32=0x9B05688C 41 const hash256_H6 uint32=0x1F83D9AB 42 const hash256_H7 uint32=0x5BE0CD19 43 44 var hash256_K = [...]uint32 { 45 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, 46 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, 47 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, 48 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, 49 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, 50 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, 51 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, 52 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2} 53 54 55 type HASH256 struct { 56 length [2]uint32 57 h [8]uint32 58 w [64]uint32 59 60 } 61 62 /* functions */ 63 func hash256_S(n uint32,x uint32) uint32 { 64 return (((x)>>n) | ((x)<<(32-n))) 65 } 66 67 func hash256_R(n uint32,x uint32) uint32 { 68 return ((x)>>n) 69 } 70 71 func hash256_Ch(x,y,z uint32) uint32 { 72 return ((x&y)^(^(x)&z)) 73 } 74 75 func hash256_Maj(x,y,z uint32) uint32 { 76 return ((x&y)^(x&z)^(y&z)) 77 } 78 79 func hash256_Sig0(x uint32) uint32 { 80 return (hash256_S(2,x)^hash256_S(13,x)^hash256_S(22,x)) 81 } 82 83 func hash256_Sig1(x uint32) uint32 { 84 return (hash256_S(6,x)^hash256_S(11,x)^hash256_S(25,x)) 85 } 86 87 func hash256_theta0(x uint32) uint32 { 88 return (hash256_S(7,x)^hash256_S(18,x)^hash256_R(3,x)); 89 } 90 91 func hash256_theta1(x uint32) uint32 { 92 return (hash256_S(17,x)^hash256_S(19,x)^hash256_R(10,x)) 93 } 94 95 func (H *HASH256) transform() { /* basic transformation step */ 96 for j:=16;j<64;j++ { 97 H.w[j]=hash256_theta1(H.w[j-2])+H.w[j-7]+hash256_theta0(H.w[j-15])+H.w[j-16] 98 } 99 a:=H.h[0]; b:=H.h[1]; c:=H.h[2]; d:=H.h[3] 100 e:=H.h[4]; f:=H.h[5]; g:=H.h[6]; hh:=H.h[7] 101 for j:=0;j<64;j++ { /* 64 times - mush it up */ 102 t1:=hh+hash256_Sig1(e)+hash256_Ch(e,f,g)+hash256_K[j]+H.w[j] 103 t2:=hash256_Sig0(a)+hash256_Maj(a,b,c) 104 hh=g; g=f; f=e 105 e=d+t1 106 d=c 107 c=b 108 b=a 109 a=t1+t2 110 } 111 H.h[0]+=a; H.h[1]+=b; H.h[2]+=c; H.h[3]+=d 112 H.h[4]+=e; H.h[5]+=f; H.h[6]+=g; H.h[7]+=hh 113 } 114 115 /* Initialise Hash function */ 116 func (H *HASH256) Init() { /* initialise */ 117 for i:=0;i<64;i++ {H.w[i]=0} 118 H.length[0]=0; H.length[1]=0 119 H.h[0]=hash256_H0 120 H.h[1]=hash256_H1 121 H.h[2]=hash256_H2 122 H.h[3]=hash256_H3 123 H.h[4]=hash256_H4 124 H.h[5]=hash256_H5 125 H.h[6]=hash256_H6 126 H.h[7]=hash256_H7 127 } 128 129 func NewHASH256() *HASH256 { 130 H:= new(HASH256) 131 H.Init() 132 return H 133 } 134 135 /* process a single byte */ 136 func (H *HASH256) Process(byt byte) { /* process the next message byte */ 137 cnt:=(H.length[0]/32)%16; 138 139 H.w[cnt]<<=8; 140 H.w[cnt]|=uint32(byt&0xFF); 141 H.length[0]+=8; 142 if H.length[0]==0 {H.length[1]++; H.length[0]=0} 143 if (H.length[0]%512)==0 {H.transform()} 144 } 145 146 /* process an array of bytes */ 147 func (H *HASH256) Process_array(b []byte) { 148 for i:=0;i<len(b);i++ {H.Process((b[i]))} 149 } 150 151 /* process a 32-bit integer */ 152 func (H *HASH256) Process_num(n int32) { 153 H.Process(byte((n>>24)&0xff)); 154 H.Process(byte((n>>16)&0xff)); 155 H.Process(byte((n>>8)&0xff)); 156 H.Process(byte(n&0xff)); 157 } 158 159 /* Generate 32-byte Hash */ 160 func (H *HASH256) Hash() []byte { /* pad message and finish - supply digest */ 161 var digest [32]byte 162 len0:=H.length[0] 163 len1:=H.length[1] 164 H.Process(0x80); 165 for (H.length[0]%512)!=448 {H.Process(0)} 166 H.w[14]=len1; 167 H.w[15]=len0; 168 H.transform(); 169 for i:=0;i<32;i++ { /* convert to bytes */ 170 digest[i]=byte((H.h[i/4]>>uint(8*(3-i%4))) & 0xff); 171 } 172 H.Init() 173 return digest[0:32] 174 } 175 176 /* test program: should produce digest */ 177 178 //248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1 179 /* 180 func main() { 181 182 test := []byte("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") 183 sh:=NewHASH256() 184 185 for i:=0;i<len(test);i++ { 186 sh.Process(test[i]) 187 } 188 189 digest:=sh.Hash() 190 for i:=0;i<32;i++ {fmt.Printf("%02x",digest[i])} 191 192 } */ 193