| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 class _Sha1 { | |
| 6 static List<int> _bytesToWords(List<int> bytes) { | |
| 7 int blocks = (((bytes.length + 1) * 8 + 64) + 511) ~/ 512; | |
| 8 List<int> words = new List<int>(blocks * 64 ~/ 4); | |
| 9 for (int i = 0; i < words.length; i++) words[i] = 0; | |
| 10 for (int i = 0, b = 0; i < bytes.length; i++, b += 8) { | |
| 11 words[b >> 5] |= (bytes[i] & 0xFF) << (24 - b % 32); | |
| 12 } | |
| 13 return words; | |
| 14 } | |
| 15 | |
| 16 /** | |
| 17 * Calculate SHA-1 hash from binary data. Based on pseudocode from | |
| 18 * http://en.wikipedia.org/wiki/SHA-1. | |
| 19 */ | |
| 20 static List<int> _hash(List<int> data) { | |
| 21 List<int> m = _bytesToWords(data); | |
| 22 int l = data.length * 8; | |
| 23 | |
| 24 // Initialize variables. | |
| 25 int h0 = 0x67452301; | |
| 26 int h1 = 0xEFCDAB89; | |
| 27 int h2 = 0x98BADCFE; | |
| 28 int h3 = 0x10325476; | |
| 29 int h4 = 0xC3D2E1F0; | |
| 30 | |
| 31 // Pre-processing. | |
| 32 m[l >> 5] |= 0x80 << (24 - l % 32); // Append 0x80. | |
| 33 m[((l + 64 >> 9) << 4) + 15] = l; // Set length at the end. | |
| 34 | |
| 35 List<int> w = new List<int>(80); | |
| 36 for (int chunk = 0; chunk < m.length; chunk += 16) { | |
| 37 // Extend the sixteen 32-bit words into eighty 32-bit words | |
| 38 for (int i = 0; i < 80; i++) { | |
| 39 if (i < 16) { | |
| 40 w[i] = m[chunk + i]; | |
| 41 } else { | |
| 42 int n = w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]; | |
| 43 w[i] = ((n << 1) | (n >> 31)) & 0xFFFFFFFF; | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 // Initialize hash value for this chunk. | |
| 48 int a = h0; | |
| 49 int b = h1; | |
| 50 int c = h2; | |
| 51 int d = h3; | |
| 52 int e = h4; | |
| 53 | |
| 54 // Main loop. | |
| 55 for (int i = 0; i < 80; i++) { | |
| 56 int f; | |
| 57 int k; | |
| 58 if (i < 20) { | |
| 59 f = b & c | ~b & d; | |
| 60 k = 0x5A827999; | |
| 61 } else if (i < 40) { | |
| 62 f = b ^ c ^ d; | |
| 63 k = 0x6ED9EBA1; | |
| 64 } else if (i < 60) { | |
| 65 f = b & c | b & d | c & d; | |
| 66 k = 0x8F1BBCDC; | |
| 67 } else { | |
| 68 f = b ^ c ^ d; | |
| 69 k = 0xCA62C1D6; | |
| 70 } | |
| 71 | |
| 72 int temp = (((a << 5) | (a >> 27)) + f + e + k + w[i]) & 0xFFFFFFFF; | |
| 73 e = d; | |
| 74 d = c; | |
| 75 c = ((b << 30) | (b >> 2)) & 0xFFFFFFFF; | |
| 76 b = a; | |
| 77 a = temp; | |
| 78 } | |
| 79 | |
| 80 // Add this chunk's hash to result so far. | |
| 81 h0 = (h0 + a) & 0xFFFFFFFF; | |
| 82 h1 = (h1 + b) & 0xFFFFFFFF; | |
| 83 h2 = (h2 + c) & 0xFFFFFFFF; | |
| 84 h3 = (h3 + d) & 0xFFFFFFFF; | |
| 85 h4 = (h4 + e) & 0xFFFFFFFF; | |
| 86 } | |
| 87 | |
| 88 // Finally return the hash as an array of bytes. | |
| 89 void intToBigEndianBytes(int value, List<int> bytes, int offset) { | |
| 90 bytes[offset] = (value >> 24) & 0xFF; | |
| 91 bytes[offset + 1] = (value >> 16) & 0xFF; | |
| 92 bytes[offset + 2] = (value >> 8) & 0xFF; | |
| 93 bytes[offset + 3] = value & 0xFF; | |
| 94 } | |
| 95 | |
| 96 List<int> result = new List<int>(20); | |
| 97 intToBigEndianBytes(h0, result, 0); | |
| 98 intToBigEndianBytes(h1, result, 4); | |
| 99 intToBigEndianBytes(h2, result, 8); | |
| 100 intToBigEndianBytes(h3, result, 12); | |
| 101 intToBigEndianBytes(h4, result, 16); | |
| 102 return result; | |
| 103 } | |
| 104 } | |
| OLD | NEW |