| 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. If [digest] or [update] are called after the first call to |
| 18 * [digest] a CryptoHashException is thrown. |
| 19 */ |
| 20 interface CryptoHash { |
| 21 /** |
| 22 * Add a list of bytes to the hash computation. |
| 23 */ |
| 24 CryptoHash update(List<int> data); |
| 10 | 25 |
| 11 final int _BITS_PER_BYTE = 8; | 26 /** |
| 12 final int _BYTES_PER_WORD = 4; | 27 * Finish the hash computation and extract the message digest as |
| 13 final int _BITS_PER_WORD = _BITS_PER_BYTE * _BYTES_PER_WORD; | 28 * a list of bytes. |
| 14 final int _MASK_32 = 0xffffffff; | 29 */ |
| 15 | 30 List<int> digest(); |
| 16 int _roundUp(int val, int n) { | |
| 17 return (val + n - 1) & -n; | |
| 18 } | 31 } |
| 19 | 32 |
| 20 // Rotate left limiting to unsigned 32-bit values. | 33 /** |
| 21 int _rotl32(int val, int shift) { | 34 * SHA1 hash function implementation. |
| 22 var mod_shift = shift & 31; | 35 */ |
| 23 return ((val << mod_shift) & _MASK_32) | | 36 interface SHA1 extends CryptoHash default _SHA1 { |
| 24 ((val & _MASK_32) >> (32 - mod_shift)); | 37 SHA1(); |
| 25 } | 38 } |
| 26 | 39 |
| 27 // Add limiting to unsigned 32-bit values. | 40 /** |
| 28 int _add32(int left, int right) { | 41 * SHA256 hash function implementation. |
| 29 return (left + right) & _MASK_32; | 42 */ |
| 43 interface SHA256 extends CryptoHash default _SHA256 { |
| 44 SHA256(); |
| 30 } | 45 } |
| 31 | 46 |
| 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 | 47 |
| 43 while (cur_out < unroll_loop_end) { | 48 /** |
| 44 var word = (input[cur++] & 0xff) << 24; | 49 * CryptoHashExceptions are thrown on invalid use of a CryptoHash |
| 45 word |= (input[cur++] & 0xff) << 16; | 50 * object. |
| 46 word |= (input[cur++] & 0xff) << 8; | 51 */ |
| 47 word |= (input[cur++] & 0xff); | 52 class CryptoHashException { |
| 48 output[cur_out++] = word; | 53 CryptoHashException(String this.message); |
| 49 } | 54 toString() => "CryptoHashException: $message"; |
| 50 // Fill the rest of the output with the remaining bytes or zeros if no more | 55 String message; |
| 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 } | 56 } |
| 62 | 57 |
| 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 |