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

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

Issue 10170027: Implement HMAC support in lib/crypto. (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
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 // Constants. 5 // Constants.
6 final _MASK_8 = 0xff; 6 final _MASK_8 = 0xff;
7 final _MASK_32 = 0xffffffff; 7 final _MASK_32 = 0xffffffff;
8 final _BITS_PER_BYTE = 8; 8 final _BITS_PER_BYTE = 8;
9 final _BYTES_PER_WORD = 4; 9 final _BYTES_PER_WORD = 4;
10 10
11 // Base class encapsulating common behavior for SHA cryptographic hash 11 // Base class encapsulating common behavior for SHA cryptographic hash
12 // functions. 12 // functions.
13 class _SHACryptoHashBase implements CryptoHash { 13 class _SHACryptoHashBase {
Ben Laurie (Google) 2012/04/27 13:26:33 Why no longer implements CryptoHash?
Mads Ager (google) 2012/04/30 07:56:57 Because it doesn't. It does not have the newInstan
14 _SHACryptoHashBase(int this._chunkSizeInWords, int this._digestSizeInWords) 14 _SHACryptoHashBase(int this._chunkSizeInWords, int this._digestSizeInWords)
15 : _pendingData = [] { 15 : _pendingData = [] {
16 _currentChunk = new List(_chunkSizeInWords); 16 _currentChunk = new List(_chunkSizeInWords);
17 _h = new List(_digestSizeInWords); 17 _h = new List(_digestSizeInWords);
18 } 18 }
19 19
20 // Update the hasher with more data. 20 // Update the hasher with more data.
21 _SHACryptoHashBase update(List<int> data) { 21 _SHACryptoHashBase update(List<int> data) {
22 if (_digestCalled) { 22 if (_digestCalled) {
23 throw new CryptoHashException( 23 throw new CryptoHashException(
(...skipping 15 matching lines...) Expand all
39 _finalizeData(); 39 _finalizeData();
40 _iterate(); 40 _iterate();
41 assert(_pendingData.length == 0); 41 assert(_pendingData.length == 0);
42 var result = []; 42 var result = [];
43 for (var i = 0; i < _h.length; i++) { 43 for (var i = 0; i < _h.length; i++) {
44 result.addAll(_wordToBytes(_h[i])); 44 result.addAll(_wordToBytes(_h[i]));
45 } 45 }
46 return result; 46 return result;
47 } 47 }
48 48
49 // Returns the block size of the hash in bytes.
50 int get blockSize() {
51 return _chunkSizeInWords * _BYTES_PER_WORD;
52 }
53
49 // One round of the hash computation. 54 // One round of the hash computation.
50 abstract _updateHash(List<int> m); 55 abstract _updateHash(List<int> m);
51 56
52 // Helper methods. 57 // Helper methods.
53 _add32(x, y) => (x + y) & _MASK_32; 58 _add32(x, y) => (x + y) & _MASK_32;
54 _roundUp(val, n) => (val + n - 1) & -n; 59 _roundUp(val, n) => (val + n - 1) & -n;
55 60
56 // Converts a list of bytes to a chunk of 32-bit words. 61 // Converts a list of bytes to a chunk of 32-bit words.
57 _bytesToChunk(List<int> data, int dataIndex) { 62 _bytesToChunk(List<int> data, int dataIndex) {
58 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD)); 63 assert((data.length - dataIndex) >= (_chunkSizeInWords * _BYTES_PER_WORD));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 var zeroPadding = finalizedLength - contentsLength; 106 var zeroPadding = finalizedLength - contentsLength;
102 for (var i = 0; i < zeroPadding; i++) { 107 for (var i = 0; i < zeroPadding; i++) {
103 _pendingData.add(0); 108 _pendingData.add(0);
104 } 109 }
105 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE; 110 var lengthInBits = _lengthInBytes * _BITS_PER_BYTE;
106 _pendingData.addAll(_wordToBytes(lengthInBits >> 32)); 111 _pendingData.addAll(_wordToBytes(lengthInBits >> 32));
107 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32)); 112 _pendingData.addAll(_wordToBytes(lengthInBits & _MASK_32));
108 } 113 }
109 114
110 // Hasher state. 115 // Hasher state.
116 final int _chunkSizeInWords;
117 final int _digestSizeInWords;
111 int _lengthInBytes = 0; 118 int _lengthInBytes = 0;
112 int _chunkSizeInWords;
113 int _digestSizeInWords;
114 List<int> _pendingData; 119 List<int> _pendingData;
115 List<int> _currentChunk; 120 List<int> _currentChunk;
116 List<int> _h; 121 List<int> _h;
117 bool _digestCalled = false; 122 bool _digestCalled = false;
118 } 123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698