Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library("crypto"); | 5 #library('crypto'); |
| 6 | 6 |
| 7 #source("sha1.dart"); | 7 #source('sha_utils.dart'); |
| 8 #source('sha1.dart'); | |
| 9 #source('sha256.dart'); | |
| 8 | 10 |
| 9 // Helpers used implementing the different crypto algorithms. | 11 /** |
| 12 * Interface for cryptographic hash functions. | |
| 13 * | |
| 14 * The [update] method is used to add data to the hash. The [digest] method | |
| 15 * is used to extract the message digest. Once the [digest] method has been | |
| 16 * called the CryptoHash object is in an invalid state and should not be | |
| 17 * used again. | |
|
Ben Laurie (Google)
2012/04/25 13:17:13
"should not"? You mean "will throw an exception"?
Mads Ager (google)
2012/04/25 13:39:34
Good point. I should throw an exception at least o
Ben Laurie (Google)
2012/04/25 13:55:43
In, e.g., HMAC I need two instances of the same ha
| |
| 18 */ | |
| 19 interface CryptoHash { | |
| 20 /** | |
| 21 * Add a list of bytes to the hash computation. | |
| 22 */ | |
| 23 CryptoHash update(List<int> data); | |
| 10 | 24 |
| 11 final int _BITS_PER_BYTE = 8; | 25 /** |
| 12 final int _BYTES_PER_WORD = 4; | 26 * Finish the hash computation and extract the message digest as |
| 13 final int _BITS_PER_WORD = _BITS_PER_BYTE * _BYTES_PER_WORD; | 27 * a list of bytes. |
| 14 final int _MASK_32 = 0xffffffff; | 28 */ |
| 15 | 29 List<int> digest(); |
| 16 int _roundUp(int val, int n) { | |
| 17 return (val + n - 1) & -n; | |
| 18 } | 30 } |
| 19 | 31 |
| 20 // Rotate left limiting to unsigned 32-bit values. | 32 /** |
| 21 int _rotl32(int val, int shift) { | 33 * SHA1 hash function implementation. |
| 22 var mod_shift = shift & 31; | 34 */ |
| 23 return ((val << mod_shift) & _MASK_32) | | 35 interface SHA1 extends CryptoHash default _SHA1 { |
| 24 ((val & _MASK_32) >> (32 - mod_shift)); | 36 SHA1(); |
| 25 } | 37 } |
| 26 | 38 |
| 27 // Add limiting to unsigned 32-bit values. | 39 /** |
| 28 int _add32(int left, int right) { | 40 * SHA256 hash function implementation. |
| 29 return (left + right) & _MASK_32; | 41 */ |
| 42 interface SHA256 extends CryptoHash default _SHA256 { | |
| 43 SHA256(); | |
| 30 } | 44 } |
| 31 | |
| 32 void _bytesToWords(List<int> input, | |
| 33 int in_offset, | |
| 34 int in_len, | |
| 35 List<int> output, | |
| 36 int out_offset, | |
| 37 int out_len) { | |
| 38 var cur = in_offset; | |
| 39 var end = in_offset + in_len; | |
| 40 var cur_out = out_offset; | |
| 41 var unroll_loop_end = in_len ~/ _BYTES_PER_WORD; | |
| 42 | |
| 43 while (cur_out < unroll_loop_end) { | |
| 44 var word = (input[cur++] & 0xff) << 24; | |
| 45 word |= (input[cur++] & 0xff) << 16; | |
| 46 word |= (input[cur++] & 0xff) << 8; | |
| 47 word |= (input[cur++] & 0xff); | |
| 48 output[cur_out++] = word; | |
| 49 } | |
| 50 // Fill the rest of the output with the remaining bytes or zeros if no more | |
| 51 // data is available. | |
| 52 while (cur_out < (out_offset + out_len)) { | |
| 53 var word = 0; | |
| 54 var bit_shift = (_BYTES_PER_WORD - 1) * _BITS_PER_BYTE; | |
| 55 while (cur < end) { | |
| 56 word |= (input[cur++] & 0xff) << bit_shift; | |
| 57 bit_shift -= _BITS_PER_BYTE; | |
| 58 } | |
| 59 output[cur_out++] = word; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 List<int> _wordsToBytes(List<int> input) { | |
| 64 var len = input.length; | |
| 65 var output = new List<int>(len * _BYTES_PER_WORD); | |
| 66 var cur = 0; | |
| 67 for (var word in input) { | |
| 68 output[cur++] = word >> 24; | |
| 69 output[cur++] = (word >> 16) & 0xff; | |
| 70 output[cur++] = (word >> 8) & 0xff; | |
| 71 output[cur++] = word & 0xff; | |
| 72 } | |
| 73 return output; | |
| 74 } | |
| OLD | NEW |