Skip to content

Commit 2af8701

Browse files
committed
sha-256 and sha-224 working
1 parent b728a09 commit 2af8701

17 files changed

+1623
-39
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/test/

dart-src/crypto.dart

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
library dart.crypto;
5+
library mycrypto;
66

77
import 'dart:math';
88

@@ -11,7 +11,7 @@ part 'hash_utils.dart';
1111
part 'hmac.dart';
1212
part 'md5.dart';
1313
part 'sha1.dart';
14-
part 'sha256.dart';
14+
part 'sha2.dart';
1515

1616
/**
1717
* Interface for cryptographic hash functions.
@@ -67,6 +67,13 @@ abstract class SHA256 implements Hash {
6767
factory SHA256() => new _SHA256();
6868
}
6969

70+
/**
71+
* SHA224 hash function implementation.
72+
*/
73+
abstract class SHA224 implements Hash {
74+
factory SHA224() => new _SHA224();
75+
}
76+
7077
/**
7178
* MD5 hash function implementation.
7279
*

dart-src/crypto_utils.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
part of dart.crypto;
5+
part of mycrypto;
66

77
abstract class _CryptoUtils {
88
static String bytesToHex(List<int> bytes) {

dart-src/hash_utils.dart

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
part of dart.crypto;
5+
part of mycrypto;
66

77
// Constants.
88
const _MASK_8 = 0xff;
@@ -24,7 +24,8 @@ int _rotl32(int val, int shift) {
2424
abstract class _HashBase implements Hash {
2525
_HashBase(int this._chunkSizeInWords,
2626
int this._digestSizeInWords,
27-
bool this._bigEndianWords)
27+
bool this._bigEndianWords,
28+
int this._resultLengthInWords)
2829
: _pendingData = [] {
2930
_currentChunk = new List(_chunkSizeInWords);
3031
_h = new List(_digestSizeInWords);
@@ -70,9 +71,10 @@ abstract class _HashBase implements Hash {
7071

7172
// Compute the final result as a list of bytes from the hash words.
7273
_resultAsBytes() {
73-
var result = [];
74-
for (var i = 0; i < _h.length; i++) {
75-
result.addAll(_wordToBytes(_h[i]));
74+
var result = new List(_resultLengthInWords * _BYTES_PER_WORD);
75+
for (var i = 0; i < _resultLengthInWords; i++) {
76+
int start = i * _BYTES_PER_WORD;
77+
result.setRange(start, start+_BYTES_PER_WORD, _wordToBytes(_h[i]));
7678
}
7779
return result;
7880
}
@@ -147,6 +149,7 @@ abstract class _HashBase implements Hash {
147149
final int _digestSizeInWords;
148150
final bool _bigEndianWords;
149151
int _lengthInBytes = 0;
152+
final int _resultLengthInWords;
150153
List<int> _pendingData;
151154
List<int> _currentChunk;
152155
List<int> _h;

dart-src/hmac.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
part of dart.crypto;
5+
part of mycrypto;
66

77
class _HMAC implements HMAC {
88
bool _isClosed = false;

dart-src/md5.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
part of dart.crypto;
5+
part of mycrypto;
66

77
// The MD5 hasher is used to compute an MD5 message digest.
88
class _MD5 extends _HashBase implements MD5 {
9-
_MD5() : super(16, 4, false) {
9+
_MD5() : super(16, 4, false, 4) {
1010
_h[0] = 0x67452301;
1111
_h[1] = 0xefcdab89;
1212
_h[2] = 0x98badcfe;

dart-src/sha1.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
part of dart.crypto;
5+
part of mycrypto;
66

77
// The SHA1 hasher is used to compute an SHA1 message digest.
88
class _SHA1 extends _HashBase implements SHA1 {
99
// Construct a SHA1 hasher object.
10-
_SHA1() : _w = new List(80), super(16, 5, true) {
10+
_SHA1() : _w = new List(80), super(16, 5, true, 5) {
1111
_h[0] = 0x67452301;
1212
_h[1] = 0xEFCDAB89;
1313
_h[2] = 0x98BADCFE;

dart-src/sha256.dart dart-src/sha2.dart

+59-26
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
part of dart.crypto;
5+
part of mycrypto;
66

7-
// The SHA256 hasher is used to compute an SHA256 message digest.
8-
class _SHA256 extends _HashBase implements SHA256 {
9-
// Construct a SHA256 hasher object.
10-
_SHA256() : _w = new List(64), super(16, 8, true) {
11-
// Initial value of the hash parts. First 32 bits of the fractional parts
12-
// of the square roots of the first 8 prime numbers.
13-
_h[0] = 0x6a09e667;
14-
_h[1] = 0xbb67ae85;
15-
_h[2] = 0x3c6ef372;
16-
_h[3] = 0xa54ff53a;
17-
_h[4] = 0x510e527f;
18-
_h[5] = 0x9b05688c;
19-
_h[6] = 0x1f83d9ab;
20-
_h[7] = 0x5be0cd19;
21-
}
227

23-
// Returns a new instance of this Hash.
24-
SHA256 newInstance() {
25-
return new SHA256();
26-
}
8+
abstract class _SHA2Base extends _HashBase {
9+
_SHA2Base(int resultLengthInWords) : _w = new List(64), super(16, 8, true, resultLengthInWords);
10+
11+
// Helper functions as defined in http://tools.ietf.org/html/rfc6234
12+
_rotr32(n, x) => (x >> n) | ((x << (32 - n)) & _MASK_32);
13+
_ch(x, y, z) => (x & y) ^ ((~x & _MASK_32) & z);
14+
_maj(x, y, z) => (x & y) ^ (x & z) ^ (y & z);
15+
16+
List<int> _w;
17+
}
2718

19+
abstract class _SHA224_256Base extends _SHA2Base {
20+
21+
_SHA224_256Base(int resultLengthInWords) : super(resultLengthInWords);
22+
2823
// Table of round constants. First 32 bits of the fractional
2924
// parts of the cube roots of the first 64 prime numbers.
3025
static const List<int> _K =
@@ -41,16 +36,13 @@ class _SHA256 extends _HashBase implements SHA256 {
4136
0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
4237
0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
4338
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 ];
44-
39+
4540
// Helper functions as defined in http://tools.ietf.org/html/rfc6234
46-
_rotr32(n, x) => (x >> n) | ((x << (32 - n)) & _MASK_32);
47-
_ch(x, y, z) => (x & y) ^ ((~x & _MASK_32) & z);
48-
_maj(x, y, z) => (x & y) ^ (x & z) ^ (y & z);
4941
_bsig0(x) => _rotr32(2, x) ^ _rotr32(13, x) ^ _rotr32(22, x);
5042
_bsig1(x) => _rotr32(6, x) ^ _rotr32(11, x) ^ _rotr32(25, x);
5143
_ssig0(x) => _rotr32(7, x) ^ _rotr32(18, x) ^ (x >> 3);
5244
_ssig1(x) => _rotr32(17, x) ^ _rotr32(19, x) ^ (x >> 10);
53-
45+
5446
// Compute one iteration of the SHA256 algorithm with a chunk of
5547
// 16 32-bit pieces.
5648
void _updateHash(List<int> M) {
@@ -100,6 +92,47 @@ class _SHA256 extends _HashBase implements SHA256 {
10092
_h[6] = _add32(g, _h[6]);
10193
_h[7] = _add32(h, _h[7]);
10294
}
95+
}
10396

104-
List<int> _w;
97+
class _SHA256 extends _SHA224_256Base implements SHA256 {
98+
99+
_SHA256() : super(8) {
100+
// Initial value of the hash parts. First 32 bits of the fractional parts
101+
// of the square roots of the first 8 prime numbers.
102+
_h[0] = 0x6a09e667;
103+
_h[1] = 0xbb67ae85;
104+
_h[2] = 0x3c6ef372;
105+
_h[3] = 0xa54ff53a;
106+
_h[4] = 0x510e527f;
107+
_h[5] = 0x9b05688c;
108+
_h[6] = 0x1f83d9ab;
109+
_h[7] = 0x5be0cd19;
110+
}
111+
112+
// Returns a new instance of this Hash.
113+
SHA256 newInstance() {
114+
return new SHA256();
115+
}
105116
}
117+
118+
class _SHA224 extends _SHA224_256Base implements SHA224 {
119+
120+
_SHA224() : super(7) {
121+
// Initial value of the hash parts. First 32 bits of the fractional parts
122+
// of the square roots of the first 8 prime numbers.
123+
_h[0] = 0xc1059ed8;
124+
_h[1] = 0x367cd507;
125+
_h[2] = 0x3070dd17;
126+
_h[3] = 0xf70e5939;
127+
_h[4] = 0xffc00b31;
128+
_h[5] = 0x68581511;
129+
_h[6] = 0x64f98fa7;
130+
_h[7] = 0xbefa4fa4;
131+
}
132+
133+
// Returns a new instance of this Hash.
134+
SHA224 newInstance() {
135+
return new SHA224();
136+
}
137+
}
138+

0 commit comments

Comments
 (0)