Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import "dart:convert"; | 7 import "dart:convert"; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
| 10 | 10 |
| 11 import "package:crypto/crypto.dart"; | 11 import "package:convert/convert.dart"; |
| 12 import "package:crypto/crypto.dart" as crypto; | |
|
nweiz
2016/04/21 19:04:38
crypto is not intended to be imported with a prefi
Bob Nystrom
2016/04/21 20:23:20
Pub defines a sha1() function so I needed to disam
| |
| 12 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
| 13 import "package:stack_trace/stack_trace.dart"; | 14 import "package:stack_trace/stack_trace.dart"; |
| 14 | 15 |
| 15 import 'exceptions.dart'; | 16 import 'exceptions.dart'; |
| 16 import 'io.dart'; | 17 import 'io.dart'; |
| 17 import 'log.dart' as log; | 18 import 'log.dart' as log; |
| 18 | 19 |
| 19 export 'asset/dart/utils.dart'; | 20 export 'asset/dart/utils.dart'; |
| 20 | 21 |
| 21 /// A regular expression matching a Dart identifier. | 22 /// A regular expression matching a Dart identifier. |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 479 | 480 |
| 480 /// Returns whether or not [str] ends with [matcher]. | 481 /// Returns whether or not [str] ends with [matcher]. |
| 481 bool endsWithPattern(String str, Pattern matcher) { | 482 bool endsWithPattern(String str, Pattern matcher) { |
| 482 for (var match in matcher.allMatches(str)) { | 483 for (var match in matcher.allMatches(str)) { |
| 483 if (match.end == str.length) return true; | 484 if (match.end == str.length) return true; |
| 484 } | 485 } |
| 485 return false; | 486 return false; |
| 486 } | 487 } |
| 487 | 488 |
| 488 /// Returns the hex-encoded sha1 hash of [source]. | 489 /// Returns the hex-encoded sha1 hash of [source]. |
| 489 String sha1(String source) { | 490 String sha1(String source) => |
| 490 var sha = new SHA1(); | 491 hex.encode(crypto.sha1.convert(source.codeUnits).bytes); |
|
nweiz
2016/04/21 19:04:38
crypto.sha1.convert(UTF8.encode(source)).toString(
Bob Nystrom
2016/04/21 20:23:20
TIL. Done. I'll send out a code review.
| |
| 491 sha.add(source.codeUnits); | 492 |
| 492 return CryptoUtils.bytesToHex(sha.close()); | 493 /// Returns the base64-encoded sha1 hash of [stream]. |
|
nweiz
2016/04/21 19:04:38
Why is this base64-encoded rather than hex-encoded
Bob Nystrom
2016/04/21 20:23:20
It's annoying that this is different from the prev
| |
| 494 Future<String> sha1Stream(Stream<List<int>> stream) async { | |
| 495 crypto.Digest digest; | |
| 496 | |
| 497 var digestSink = new ChunkedConversionSink<crypto.Digest>.withCallback( | |
| 498 (digests) { | |
| 499 digest = digests.single; | |
| 500 }); | |
| 501 | |
| 502 var byteSink = crypto.sha1.startChunkedConversion(digestSink); | |
| 503 | |
| 504 await stream.forEach((chunk) { | |
| 505 byteSink.add(chunk); | |
| 506 }); | |
| 507 | |
| 508 byteSink.close(); | |
| 509 | |
| 510 // TODO(rnystrom): this call to `close` should not be needed. Remove when | |
| 511 // https://github.com/dart-lang/crypto/issues/33 | |
| 512 // is fixed. | |
| 513 // Does not cause any problems in the mean time. | |
| 514 digestSink.close(); | |
| 515 | |
| 516 return BASE64.encode(digest.bytes); | |
| 493 } | 517 } |
| 494 | 518 |
| 495 /// Configures [future] so that its result (success or exception) is passed on | 519 /// Configures [future] so that its result (success or exception) is passed on |
| 496 /// to [completer]. | 520 /// to [completer]. |
| 497 void chainToCompleter(Future future, Completer completer) { | 521 void chainToCompleter(Future future, Completer completer) { |
| 498 future.then(completer.complete, onError: completer.completeError); | 522 future.then(completer.complete, onError: completer.completeError); |
| 499 } | 523 } |
| 500 | 524 |
| 501 /// Ensures that [stream] can emit at least one value successfully (or close | 525 /// Ensures that [stream] can emit at least one value successfully (or close |
| 502 /// without any values). | 526 /// without any values). |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 948 } else { | 972 } else { |
| 949 throw new ApplicationException(message); | 973 throw new ApplicationException(message); |
| 950 } | 974 } |
| 951 } | 975 } |
| 952 | 976 |
| 953 /// Throw a [DataException] with [message] to indicate that the command has | 977 /// Throw a [DataException] with [message] to indicate that the command has |
| 954 /// failed because of invalid input data. | 978 /// failed because of invalid input data. |
| 955 /// | 979 /// |
| 956 /// This will report the error and cause pub to exit with [exit_codes.DATA]. | 980 /// This will report the error and cause pub to exit with [exit_codes.DATA]. |
| 957 void dataError(String message) => throw new DataException(message); | 981 void dataError(String message) => throw new DataException(message); |
| OLD | NEW |