| OLD | NEW |
| (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 import 'dart:convert'; |
| 6 import 'dart:typed_data'; |
| 7 |
| 8 import 'digest.dart'; |
| 9 import 'digest_sink.dart'; |
| 10 import 'hash.dart'; |
| 11 |
| 12 /// An implementation of [keyed-hash method authentication codes][rfc]. |
| 13 /// |
| 14 /// [rfc]: https://tools.ietf.org/html/rfc2104 |
| 15 /// |
| 16 /// HMAC allows messages to be cryptographically authenticated using any |
| 17 /// iterated cryptographic hash function. |
| 18 class Hmac extends Converter<List<int>, Digest> { |
| 19 /// The hash function used to compute the authentication digest. |
| 20 final Hash _hash; |
| 21 |
| 22 /// The secret key shared by the sender and the receiver. |
| 23 final Uint8List _key; |
| 24 |
| 25 /// Create an [Hmac] object from a [Hash] and a binary key. |
| 26 /// |
| 27 /// The key should be a secret shared between the sender and receiver of the |
| 28 /// message. |
| 29 Hmac(Hash hash, List<int> key) |
| 30 : _hash = hash, |
| 31 _key = new Uint8List(hash.blockSize) { |
| 32 // Hash the key if it's longer than the block size of the hash. |
| 33 if (key.length > _hash.blockSize) key = _hash.convert(key).bytes; |
| 34 |
| 35 // If [key] is shorter than the block size, the rest of [_key] will be |
| 36 // 0-padded. |
| 37 _key.setRange(0, key.length, key); |
| 38 } |
| 39 |
| 40 @override |
| 41 Digest convert(List<int> data) { |
| 42 var innerSink = new DigestSink(); |
| 43 var outerSink = startChunkedConversion(innerSink); |
| 44 outerSink.add(data); |
| 45 outerSink.close(); |
| 46 return innerSink.value; |
| 47 } |
| 48 |
| 49 @override |
| 50 ByteConversionSink startChunkedConversion(Sink<Digest> sink) => |
| 51 new _HmacSink(sink, _hash, _key); |
| 52 } |
| 53 |
| 54 /// The concrete implementation of the HMAC algorithm. |
| 55 class _HmacSink extends ByteConversionSink { |
| 56 /// The sink for the outer hash computation. |
| 57 final ByteConversionSink _outerSink; |
| 58 |
| 59 /// The sink that [_innerSink]'s result will be added to when it's available. |
| 60 final _innerResultSink = new DigestSink(); |
| 61 |
| 62 /// The sink for the inner hash computation. |
| 63 ByteConversionSink _innerSink; |
| 64 |
| 65 /// Whether [close] has been called. |
| 66 bool _isClosed = false; |
| 67 |
| 68 _HmacSink(Sink<Digest> sink, Hash hash, List<int> key) |
| 69 : _outerSink = hash.startChunkedConversion(sink) { |
| 70 _innerSink = hash.startChunkedConversion(_innerResultSink); |
| 71 |
| 72 // Compute outer padding. |
| 73 var padding = new Uint8List(key.length); |
| 74 for (var i = 0; i < padding.length; i++) { |
| 75 padding[i] = 0x5c ^ key[i]; |
| 76 } |
| 77 _outerSink.add(padding); |
| 78 |
| 79 // Compute inner padding. |
| 80 for (var i = 0; i < padding.length; i++) { |
| 81 padding[i] = 0x36 ^ key[i]; |
| 82 } |
| 83 _innerSink.add(padding); |
| 84 } |
| 85 |
| 86 @override |
| 87 void add(List<int> data) { |
| 88 if (_isClosed) throw new StateError("HMAC is closed"); |
| 89 _innerSink.add(data); |
| 90 } |
| 91 |
| 92 @override |
| 93 void addSlice(List<int> data, int start, int end, bool isLast) { |
| 94 if (_isClosed) throw new StateError("HMAC is closed"); |
| 95 _innerSink.addSlice(data, start, end, isLast); |
| 96 } |
| 97 |
| 98 @override |
| 99 void close() { |
| 100 if (_isClosed) return; |
| 101 _isClosed = true; |
| 102 |
| 103 _innerSink.close(); |
| 104 _outerSink.add(_innerResultSink.value.bytes); |
| 105 _outerSink.close(); |
| 106 } |
| 107 } |
| OLD | NEW |