OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:io' show exit, File; | 5 import 'dart:async'; |
6 import 'package:crypto/crypto.dart' show MD5, SHA1, SHA256, CryptoUtils; | 6 import 'dart:io'; |
7 | 7 |
8 const USAGE = 'Usage: dart hash.dart <md5|sha1|sha256> <input_filename>'; | 8 import 'package:crypto/crypto.dart'; |
9 | 9 |
10 main(List<String> args) async { | 10 final _usage = 'Usage: dart hash.dart <md5|sha1|sha256> <input_filename>'; |
| 11 |
| 12 Future main(List<String> args) async { |
11 if (args == null || args.length != 2) { | 13 if (args == null || args.length != 2) { |
12 print(USAGE); | 14 print(_usage); |
13 exit(1); | 15 exit(1); |
14 } | 16 } |
15 | 17 |
16 var hasher; | 18 Hash hasher; |
17 | 19 |
18 switch (args[0]) { | 20 switch (args[0]) { |
19 case 'md5': | 21 case 'md5': |
20 hasher = new MD5(); | 22 hasher = md5; |
21 break; | 23 break; |
22 case 'sha1': | 24 case 'sha1': |
23 hasher = new SHA1(); | 25 hasher = sha1; |
24 break; | 26 break; |
25 case 'sha256': | 27 case 'sha256': |
26 hasher = new SHA256(); | 28 hasher = sha256; |
27 break; | 29 break; |
28 default: | 30 default: |
29 print(USAGE); | 31 print(_usage); |
30 exit(1); | 32 exit(1); |
31 } | 33 } |
32 | 34 |
33 var filename = args[1]; | 35 var filename = args[1]; |
34 var input = new File(filename); | 36 var input = new File(filename); |
35 | 37 |
36 if (!input.existsSync()) { | 38 if (!input.existsSync()) { |
37 print('File "$filename" does not exist.'); | 39 print('File "$filename" does not exist.'); |
38 exit(1); | 40 exit(1); |
39 } | 41 } |
40 | 42 |
41 await for (var bytes in input.openRead()) { | 43 var value = await hasher.bind(input.openRead()).first; |
42 hasher.add(bytes); | |
43 } | |
44 | 44 |
45 var hex = CryptoUtils.bytesToHex(hasher.close()); | 45 print(value); |
46 | |
47 print(hex); | |
48 } | 46 } |
OLD | NEW |