Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(438)

Side by Side Diff: lib/crypto/sha_utils.dart

Issue 10134055: Add common interface for cryptographic hash functions to lib/crypto.dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/crypto/sha256.dart ('k') | tests/lib/src/crypto/sha1Test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Constants.
6 final _MASK_8 = 0xff;
7 final _MASK_32 = 0xffffffff;
8 final _BITS_PER_BYTE = 8;
9 final _BYTES_PER_WORD = 4;
10
11 // Base class encapsulating common behavior for SHA cryptographic hash
12 // functions.
13 class _SHACryptoHashBase implements CryptoHash {
14 _SHACryptoHashBase(int this._chunkSizeInWords, int this._digestSizeInWords)
15 : _pendingData = [] {
16 _currentChunk = new List(_chunkSizeInWords);
17 _h = new List(_digestSizeInWords);
18 }
19
20 // Update the hasher with more data.
21 _SHACryptoHashBase update(List<int> data) {
22 if (_digestCalled) {
23 throw new CryptoHashException(
24 'CryptoHash update method called after digest');
25 }
26 _lengthInBytes += data.length;
27 _pendingData.addAll(data);
28 _iterate();
29 return this;
30 }
31
32 // Finish the hash computation and return the digest string.
33 List<int> digest() {
34 if (_digestCalled) {
35 throw new CryptoHashException(
36 'CryptoHash digest method called more than once');
37 }
38 _digestCalled = true;
39 _finalizeData();
40 _iterate();
41 assert(_pendingData.length == 0);
42 var result = [];
43 for (var i = 0; i < _h.length; i++) {
44 result.addAll(_wordToBytes(_h[i]));
45 }
46 return result;
47 }
48
49 // One round of the hash computation.
50 abstract _updateHash(List<int> m);
51
52 // Helper methods.
53 _add32(x, y) => (x + y) & _MASK_32;
54 _roundUp(val, n) => (val + n - 1) & -n;
55
56 // Converts a list of bytes to a chunk of 32-bit words.
57 _bytesToChunk(List<int> data, int dataIndex) {
58 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD));
59 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) {
60 var word = (data[dataIndex++] & 0xff) << 24;
61 word |= (data[dataIndex++] & _MASK_8) << 16;
62 word |= (data[dataIndex++] & _MASK_8) << 8;
63 word |= (data[dataIndex++] & _MASK_8);
64 _currentChunk[wordIndex] = word;
65 }
66 }
67
68 // Convert a 32-bit word to four bytes.
69 _wordToBytes(int word) {
70 List<int> bytes = new List(_BYTES_PER_WORD);
71 bytes[0] = word >> 24;
72 bytes[1] = (word >> 16) & _MASK_8;
73 bytes[2] = (word >> 8) & _MASK_8;
74 bytes[3] = word & _MASK_8;
75 return bytes;
76 }
77
78 // Iterate through data updating the hash computation for each
79 // chunk.
80 _iterate() {
81 var len = _pendingData.length;
82 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
83 if (len >= chunkSizeInBytes) {
84 var index = 0;
85 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) {
86 _bytesToChunk(_pendingData, index);
87 _updateHash(_currentChunk);
88 }
89 var remaining = len - index;
90 _pendingData = _pendingData.getRange(index, remaining);
91 }
92 }
93
94 // Finalize the data. Add a 1 bit to the end of the message. Expand with
95 // 0 bits and the length of the message.
96 _finalizeData() {
97 _pendingData.add(0x80);
98 var contentsLength = _lengthInBytes + 9;
99 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
100 var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes);
101 var zeroPadding = finalizedLength - contentsLength;
102 for (var i = 0; i < zeroPadding; i++) {
103 _pendingData.add(0);
104 }
105 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE;
106 _pendingData.addAll(_wordToBytes(lengthInBits >> 32));
107 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
108 }
109
110 // Hasher state.
111 int _lengthInBytes = 0;
112 int _chunkSizeInWords;
113 int _digestSizeInWords;
114 List<int> _pendingData;
115 List<int> _currentChunk;
116 List<int> _h;
117 bool _digestCalled = false;
118 }
OLDNEW
« no previous file with comments | « lib/crypto/sha256.dart ('k') | tests/lib/src/crypto/sha1Test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698