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

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: 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
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 _lengthInBytes += data.length;
23 _pendingData.addAll(data);
24 _iterate();
25 return this;
26 }
27
28 // Finish the hash computation and return the digest string.
29 List<int> digest() {
30 _finalizeData();
31 _iterate();
32 assert(_pendingData.length == 0);
33 var result = [];
34 for (var i = 0; i < _h.length; i++) {
35 result.addAll(_wordToBytes(_h[i]));
36 }
37 return result;
38 }
39
40 // One round of the hash computation.
41 abstract _updateHash(List<int> m);
42
43 // Helper methods.
44 _add32(x, y) => (x + y) & _MASK_32;
45 _roundUp(val, n) => (val + n - 1) & -n;
46
47 // Converts a list of bytes to a chunk of 32-bit words.
48 _bytesToChunk(List<int> data, int dataIndex) {
49 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD));
50 for (var wordIndex = 0; wordIndex < _chunkSizeInWords; wordIndex++) {
51 var word = (data[dataIndex++] & 0xff) << 24;
52 word |= (data[dataIndex++] & _MASK_8) << 16;
53 word |= (data[dataIndex++] & _MASK_8) << 8;
54 word |= (data[dataIndex++] & _MASK_8);
55 _currentChunk[wordIndex] = word;
56 }
57 }
58
59 // Convert a 32-bit word to four bytes.
60 _wordToBytes(int word) {
61 List<int> bytes = new List(_BYTES_PER_WORD);
62 bytes[0] = word >> 24;
63 bytes[1] = (word >> 16) & _MASK_8;
64 bytes[2] = (word >> 8) & _MASK_8;
65 bytes[3] = word & _MASK_8;
66 return bytes;
67 }
68
69 // Iterate through data updating the hash computation for each
70 // chunk.
71 _iterate() {
72 var len = _pendingData.length;
73 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
74 if (len >= chunkSizeInBytes) {
75 var index = 0;
76 for (; (len - index) >= chunkSizeInBytes; index += chunkSizeInBytes) {
77 _bytesToChunk(_pendingData, index);
78 _updateHash(_currentChunk);
79 }
80 var remaining = len - index;
81 _pendingData = _pendingData.getRange(index, remaining);
82 }
83 }
84
85 // Finalize the data. Add a 1 bit to the end of the message. Expand with
86 // 0 bits and the length of the message.
87 _finalizeData() {
88 _pendingData.add(0x80);
89 var contentsLength = _lengthInBytes + 9;
90 var chunkSizeInBytes = _chunkSizeInWords * _BYTES_PER_WORD;
91 var finalizedLength = _roundUp(contentsLength, chunkSizeInBytes);
92 var zeroPadding = finalizedLength - contentsLength;
93 for (var i = 0; i < zeroPadding; i++) {
94 _pendingData.add(0);
95 }
96 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE;
97 _pendingData.addAll(_wordToBytes(lengthInBits >> 32));
98 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
99 }
100
101 // Hasher state.
102 int _lengthInBytes = 0;
103 int _chunkSizeInWords;
104 int _digestSizeInWords;
105 List<int> _pendingData;
106 List<int> _currentChunk;
107 List<int> _h;
108 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698