Class: EsuApi::SHA0
- Inherits:
-
Object
- Object
- EsuApi::SHA0
- Defined in:
- lib/EsuApi.rb
Constant Summary collapse
- BLOCK_SIZE =
64
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#counter ⇒ Object
Returns the value of attribute counter.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
-
#clone ⇒ Object
Creates a deep copy of the object.
-
#f1(a, b, c) ⇒ Object
Round 1 mix function.
-
#f2(a, b, c) ⇒ Object
Round 2 mix function.
-
#f3(a, b, c) ⇒ Object
Round 3 mix function.
-
#f4(a, b, c) ⇒ Object
Round 4 mix function.
- #hashFinal(data) ⇒ Object
- #hashUpdate(data) ⇒ Object
-
#initialize ⇒ SHA0
constructor
A new instance of SHA0.
- #internalHashUpdate(data) ⇒ Object
-
#rol(val, steps) ⇒ Object
Roll left; truncate to 32 bits.
-
#truncadd(v1, v2, v3 = 0, v4 = 0) ⇒ Object
Truncate-and-add function.
-
#truncate(val) ⇒ Object
Truncates to 32 bits to prevent Fixnum from becoming Bignum.
Constructor Details
#initialize ⇒ SHA0
Returns a new instance of SHA0.
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 |
# File 'lib/EsuApi.rb', line 1069 def initialize() @state = [] @constants = [] @buffer = "" @state[0] = 0x67452301 @state[1] = 0xefcdab89 @state[2] = 0x98badcfe @state[3] = 0x10325476 @state[4] = 0xc3d2e1f0 @constants[0] = 0x5a827999 @constants[1] = 0x6ed9eba1 @constants[2] = 0x8f1bbcdc @constants[3] = 0xca62c1d6 @counter = 0 end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
1407 1408 1409 |
# File 'lib/EsuApi.rb', line 1407 def buffer @buffer end |
#counter ⇒ Object
Returns the value of attribute counter.
1407 1408 1409 |
# File 'lib/EsuApi.rb', line 1407 def counter @counter end |
#state ⇒ Object
Returns the value of attribute state.
1407 1408 1409 |
# File 'lib/EsuApi.rb', line 1407 def state @state end |
Instance Method Details
#clone ⇒ Object
Creates a deep copy of the object. This allows you to get the current hash value at various offsets without disrupting the hash in progress, e.g. sha = EsuApi::SHA0.new() sha.hashUpdate( block1 ) shacopy = sha.clone() partialHash = shacopy.hashFinal(nil) sha.hashUpdate( block2 ) ...
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
# File 'lib/EsuApi.rb', line 1098 def clone() copy = EsuApi::SHA0.new() copy.state[0] = @state[0] copy.state[1] = @state[1] copy.state[2] = @state[2] copy.state[3] = @state[3] copy.state[4] = @state[4] copy.counter = @counter copy.buffer = @buffer+"" # Clone the string return copy end |
#f1(a, b, c) ⇒ Object
Round 1 mix function
1388 1389 1390 |
# File 'lib/EsuApi.rb', line 1388 def f1( a, b, c ) return truncate((c^(a&(b^c))) + @constants[0]) end |
#f2(a, b, c) ⇒ Object
Round 2 mix function
1393 1394 1395 |
# File 'lib/EsuApi.rb', line 1393 def f2( a, b, c ) return truncate((a^b^c) + @constants[1]) end |
#f3(a, b, c) ⇒ Object
Round 3 mix function
1398 1399 1400 |
# File 'lib/EsuApi.rb', line 1398 def f3( a, b, c ) return truncate(((a&b)|(c&(a|b))) + @constants[2]) end |
#f4(a, b, c) ⇒ Object
Round 4 mix function
1403 1404 1405 |
# File 'lib/EsuApi.rb', line 1403 def f4( a, b, c ) return truncate((a^b^c) + @constants[3]) end |
#hashFinal(data) ⇒ Object
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 |
# File 'lib/EsuApi.rb', line 1135 def hashFinal( data ) if( data == nil ) data = "" end # Consume up to the last block hashUpdate( data ) @counter += @buffer.length << 3 # Append the bits 1000 0000 @buffer += 0x80.chr # See if we have enough room to pad out the final block if( @buffer.length > SHA0::BLOCK_SIZE-8 ) while( @buffer.length < SHA0::BLOCK_SIZE ) @buffer+=0.chr end internalHashUpdate( @buffer ) @buffer = "" # Write a zero buffer. (0..SHA0::BLOCK_SIZE-9).each{ |i| @buffer[i] = 0.chr } end # Expand the buffer out to a block size while( @buffer.length < SHA0::BLOCK_SIZE-8 ) @buffer += 0.chr end # Append the bit count (8 bytes) to buffer carr = [] carr.push( 0 ) carr.push( @counter ) @buffer += carr.pack( "N*" ) # Process the final block internalHashUpdate( @buffer ) # var output:ByteArray = new ByteArray() # output.writeUnsignedInt( state[0] ) # output.writeUnsignedInt( state[1] ) # output.writeUnsignedInt( state[2] ) # output.writeUnsignedInt( state[3] ) # output.writeUnsignedInt( state[4] ) output = @state.pack( "N*" ) return output end |
#hashUpdate(data) ⇒ Object
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 |
# File 'lib/EsuApi.rb', line 1114 def hashUpdate( data ) # Break up into 64 byte chunks. i=0 while( i<data.length ) if( data.length - i + @buffer.length >= BLOCK_SIZE ) usedBytes = SHA0::BLOCK_SIZE-@buffer.length @buffer += data.slice( i, usedBytes ) internalHashUpdate( @buffer ) @counter += SHA0::BLOCK_SIZE << 3 @buffer = "" i+= usedBytes else # Save remaining bytes for next chunk @buffer += data.slice( i, data.length-i ) i += data.length-i end end end |
#internalHashUpdate(data) ⇒ Object
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 |
# File 'lib/EsuApi.rb', line 1186 def internalHashUpdate( data ) # Expand the buffer into an array of uints nblk = data.unpack( "N*" ) # Expand into an array of 80 uints (16..79).each{ |i| nblk[i] = nblk[i-3] ^ nblk[i-8] ^ nblk[i-14] ^ nblk[i-16] } # Do the rounds a = truncate(@state[0]) b = truncate(@state[1]) c = truncate(@state[2]) d = truncate(@state[3]) e = truncate(@state[4]) e = truncadd( e, rol( a, 5 ) + f1(b, c, d) + nblk[0] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f1(a, b, c) + nblk[1] ) a =rol( a, 30 ) c = c = truncadd( c, rol( d, 5 ) + f1(e, a, b) + nblk[2] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f1(d, e, a) + nblk[3] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f1(c, d, e) + nblk[4] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f1(b, c, d) + nblk[5] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f1(a, b, c) + nblk[6] ) a =rol( a, 30 ) c = c = truncadd( c, rol( d, 5 ) + f1(e, a, b) + nblk[7] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f1(d, e, a) + nblk[8] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f1(c, d, e) + nblk[9] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f1(b, c, d) + nblk[10] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f1(a, b, c) + nblk[11] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f1(e, a, b) + nblk[12] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f1(d, e, a) + nblk[13] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f1(c, d, e) + nblk[14] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f1(b, c, d) + nblk[15] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f1(a, b, c) + nblk[16] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f1(e, a, b) + nblk[17] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f1(d, e, a) + nblk[18] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f1(c, d, e) + nblk[19] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f2(b, c, d) + nblk[20] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f2(a, b, c) + nblk[21] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f2(e, a, b) + nblk[22] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f2(d, e, a) + nblk[23] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f2(c, d, e) + nblk[24] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f2(b, c, d) + nblk[25] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f2(a, b, c) + nblk[26] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f2(e, a, b) + nblk[27] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f2(d, e, a) + nblk[28] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f2(c, d, e) + nblk[29] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f2(b, c, d) + nblk[30] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f2(a, b, c) + nblk[31] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f2(e, a, b) + nblk[32] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f2(d, e, a) + nblk[33] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f2(c, d, e) + nblk[34] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f2(b, c, d) + nblk[35] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f2(a, b, c) + nblk[36] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f2(e, a, b) + nblk[37] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f2(d, e, a) + nblk[38] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f2(c, d, e) + nblk[39] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f3(b, c, d) + nblk[40] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f3(a, b, c) + nblk[41] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f3(e, a, b) + nblk[42] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f3(d, e, a) + nblk[43] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f3(c, d, e) + nblk[44] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f3(b, c, d) + nblk[45] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f3(a, b, c) + nblk[46] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f3(e, a, b) + nblk[47] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f3(d, e, a) + nblk[48] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f3(c, d, e) + nblk[49] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f3(b, c, d) + nblk[50] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f3(a, b, c) + nblk[51] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f3(e, a, b) + nblk[52] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f3(d, e, a) + nblk[53] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f3(c, d, e) + nblk[54] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f3(b, c, d) + nblk[55] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f3(a, b, c) + nblk[56] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f3(e, a, b) + nblk[57] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f3(d, e, a) + nblk[58] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f3(c, d, e) + nblk[59] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f4(b, c, d) + nblk[60] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f4(a, b, c) + nblk[61] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f4(e, a, b) + nblk[62] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f4(d, e, a) + nblk[63] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f4(c, d, e) + nblk[64] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f4(b, c, d) + nblk[65] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f4(a, b, c) + nblk[66] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f4(e, a, b) + nblk[67] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f4(d, e, a) + nblk[68] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f4(c, d, e) + nblk[69] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f4(b, c, d) + nblk[70] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f4(a, b, c) + nblk[71] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f4(e, a, b) + nblk[72] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f4(d, e, a) + nblk[73] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f4(c, d, e) + nblk[74] ) c =rol( c, 30 ) e = truncadd( e, rol( a, 5 ) + f4(b, c, d) + nblk[75] ) b =rol( b, 30 ) d = d = truncadd( d, rol( e, 5 ) + f4(a, b, c) + nblk[76] ) a =rol( a, 30 ) c = truncadd( c, rol( d, 5 ) + f4(e, a, b) + nblk[77] ) e =rol( e, 30 ) b = truncadd( b, rol( c, 5 ) + f4(d, e, a) + nblk[78] ) d =rol( d, 30 ) a = truncadd( a, rol( b, 5 ) + f4(c, d, e) + nblk[79] ) c =rol( c, 30 ) # Update state @state[0] = truncate( a + @state[0] ) @state[1] = truncate( b + @state[1] ) @state[2] = truncate( c + @state[2] ) @state[3] = truncate( d + @state[3] ) @state[4] = truncate( e + @state[4] ) end |
#rol(val, steps) ⇒ Object
Roll left; truncate to 32 bits.
1373 1374 1375 |
# File 'lib/EsuApi.rb', line 1373 def rol( val, steps ) return truncate( val << steps )|truncate( val >> 32-steps ) end |
#truncadd(v1, v2, v3 = 0, v4 = 0) ⇒ Object
Truncate-and-add function
1383 1384 1385 |
# File 'lib/EsuApi.rb', line 1383 def truncadd( v1, v2, v3=0, v4=0 ) return truncate( v1+v2+v3+v4 ) end |
#truncate(val) ⇒ Object
Truncates to 32 bits to prevent Fixnum from becoming Bignum
1378 1379 1380 |
# File 'lib/EsuApi.rb', line 1378 def truncate( val ) return val & 0xffffffff end |