Skip to content

Commit

Permalink
Calculates the MD5 digest and returns the value
Browse files Browse the repository at this point in the history
The method name was not really explicit. Indeed, we
expect the method to return the md5 digest but it
returns something else.
  • Loading branch information
eeppuJ committed Dec 15, 2019
1 parent e70b9b1 commit 194cf1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
13 changes: 8 additions & 5 deletions lib/src/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ extension StringX on String {

List<int> toUtf16() => codeUnits;

String get md5 {
var utf8 = toUtf8();
var digest = crypto.md5.convert(utf8);
return String.fromCharCodes(digest.bytes);
}
/// Calculates the MD5 digest and returns the value as a [String] of
/// hexadecimal digits.
///
/// ```dart
/// print('abc'.md5); //900150983cd24fb0d6963f7d28e17f72
/// print('message digest'.md5); //f96b697d7cb7938d525a2f31aaf161d0
/// ```
String get md5 => crypto.md5.convert(toUtf8()).toString();
}
23 changes: 22 additions & 1 deletion test/string_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';

import 'package:test/test.dart';
import 'package:dartx/dartx.dart';
import 'package:test/test.dart';

void main() {
group('StringX', () {
Expand Down Expand Up @@ -121,5 +121,26 @@ void main() {
expect('hello'.toUtf16(), 'hello'.codeUnits);
expect('ഐ⌛酪Б👨‍👨‍👧‍👦'.toUtf16(), 'ഐ⌛酪Б👨‍👨‍👧‍👦'.codeUnits);
});

test('.md5)', () {
expect(''.md5, 'd41d8cd98f00b204e9800998ecf8427e');
expect('a'.md5, '0cc175b9c0f1b6a831c399e269772661');
expect('abc'.md5, '900150983cd24fb0d6963f7d28e17f72');
expect('message digest'.md5, 'f96b697d7cb7938d525a2f31aaf161d0');
expect('ഐ⌛酪Б👨‍👨‍👧‍👦'.md5, 'c7834eff7c967101cfb65b8f6d15ad46');
expect(
'abcdefghijklmnopqrstuvwxyz'.md5,
'c3fcd3d76192e4007dfb496cca67e13b'
);
expect(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.md5,
'd174ab98d277d9f5a5611c2c9f419d9f'
);
expect(
'12345678901234567890123456789012345678901234567890123456789012'
'345678901234567890'.md5,
'57edf4a22be3c955ac49da2e2107b67a'
);
});
});
}

0 comments on commit 194cf1a

Please sign in to comment.