| 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 /** | 5 /** |
| 6 * SHA-1. | 6 * SHA-1. |
| 7 * Ripped from package:crypto. | 7 * Ripped from package:crypto. |
| 8 */ | 8 */ |
| 9 library sha1; | 9 library sha1; |
| 10 | 10 |
| 11 import 'dart:math' show pow; | 11 import 'dart:math' show pow; |
| 12 import 'dart:convert'; | 12 import 'dart:convert'; |
| 13 | 13 |
| 14 /// Returns the base64-encoded SHA-1 hash of the utf-8 bytes of [input]. | 14 import '../io/code_output.dart' show CodeOutputListener; |
| 15 String hashOfString(String input) { | |
| 16 Hash hasher = new SHA1(); | |
| 17 hasher.add(const Utf8Encoder().convert(input)); | |
| 18 return _bytesToBase64(hasher.close()); | |
| 19 } | |
| 20 | 15 |
| 21 /** | 16 class Hasher implements CodeOutputListener { |
| 22 * Converts a list of bytes into a Base 64 encoded string. | 17 Hash _hasher = new SHA1(); |
| 23 * | 18 String _hashString; |
| 24 * The list can be any list of integers in the range 0..255, | 19 |
| 25 * for example a message digest. | 20 @override |
| 26 * | 21 void onDone(int length) { |
| 27 * If [addLineSeparator] is true, the resulting string will be | 22 // Do nothing. |
| 28 * broken into lines of 76 characters, separated by "\r\n". | 23 } |
| 29 * | 24 |
| 30 * If [urlSafe] is true, the result is URL and filename safe. | 25 @override |
| 31 * | 26 void onText(String text) { |
| 32 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) | 27 if (_hasher != null) { |
| 33 * | 28 _hasher.add(const Utf8Encoder().convert(text)); |
| 34 */ | 29 } |
| 35 String _bytesToBase64(List<int> bytes, | 30 } |
| 36 {bool urlSafe : false, | 31 |
| 37 bool addLineSeparator : false}) { | 32 /// Returns the base64-encoded SHA-1 hash of the utf-8 bytes of the output |
| 38 return _CryptoUtils.bytesToBase64(bytes, | 33 /// text. |
| 39 urlSafe, | 34 String getHash() { |
| 40 addLineSeparator); | 35 if (_hashString == null) { |
| 36 _hashString = _bytesToBase64(_hasher.close()); |
| 37 _hasher = null; |
| 38 } |
| 39 return _hashString; |
| 40 } |
| 41 |
| 42 /** |
| 43 * Converts a list of bytes into a Base 64 encoded string. |
| 44 * |
| 45 * The list can be any list of integers in the range 0..255, |
| 46 * for example a message digest. |
| 47 * |
| 48 * If [addLineSeparator] is true, the resulting string will be |
| 49 * broken into lines of 76 characters, separated by "\r\n". |
| 50 * |
| 51 * If [urlSafe] is true, the result is URL and filename safe. |
| 52 * |
| 53 * Based on [RFC 4648](http://tools.ietf.org/html/rfc4648) |
| 54 * |
| 55 */ |
| 56 String _bytesToBase64(List<int> bytes, |
| 57 {bool urlSafe : false, |
| 58 bool addLineSeparator : false}) { |
| 59 return _CryptoUtils.bytesToBase64(bytes, |
| 60 urlSafe, |
| 61 addLineSeparator); |
| 62 } |
| 41 } | 63 } |
| 42 | 64 |
| 43 | 65 |
| 44 // Constants. | 66 // Constants. |
| 45 const _MASK_8 = 0xff; | 67 const _MASK_8 = 0xff; |
| 46 const _MASK_32 = 0xffffffff; | 68 const _MASK_32 = 0xffffffff; |
| 47 const _BITS_PER_BYTE = 8; | 69 const _BITS_PER_BYTE = 8; |
| 48 const _BYTES_PER_WORD = 4; | 70 const _BYTES_PER_WORD = 4; |
| 49 | 71 |
| 50 // Helper functions used by more than one hasher. | 72 // Helper functions used by more than one hasher. |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 int y = bytes[i + 1]; | 390 int y = bytes[i + 1]; |
| 369 out[j++] = lookup.codeUnitAt(x >> 2); | 391 out[j++] = lookup.codeUnitAt(x >> 2); |
| 370 out[j++] = lookup.codeUnitAt(((x << 4) | (y >> 4)) & 0x3F); | 392 out[j++] = lookup.codeUnitAt(((x << 4) | (y >> 4)) & 0x3F); |
| 371 out[j++] = lookup.codeUnitAt((y << 2) & 0x3F); | 393 out[j++] = lookup.codeUnitAt((y << 2) & 0x3F); |
| 372 out[j++] = PAD; | 394 out[j++] = PAD; |
| 373 } | 395 } |
| 374 | 396 |
| 375 return new String.fromCharCodes(out); | 397 return new String.fromCharCodes(out); |
| 376 } | 398 } |
| 377 } | 399 } |
| OLD | NEW |