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