Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(604)

Unified Diff: lib/crypto/crypto_utils.dart

Issue 10456028: Add base 64 encoding to the crypto library utils. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/crypto/crypto_utils.dart
diff --git a/lib/crypto/crypto_utils.dart b/lib/crypto/crypto_utils.dart
index aa364b68bdcc6e4cfec6d2a01c2eef27d1cde8ea..1b9ebff362e9579cb2df4daf9120da7c7fc42e26 100644
--- a/lib/crypto/crypto_utils.dart
+++ b/lib/crypto/crypto_utils.dart
@@ -10,4 +10,46 @@ class _CryptoUtils {
}
return result.toString();
}
+
+ static String bytesToBase64(List<int> bytes) {
+ final table =
+ const [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
+ 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', '+', '/' ];
+
+ var result = new StringBuffer();
+
+ // Encode all full 24-bit blocks.
+ var i = 0;
+ for (; (i + 2) < bytes.length; i += 3) {
+ var b0 = bytes[i] & 0xff;
+ var b1 = bytes[i + 1] & 0xff;
+ var b2 = bytes[i + 2] & 0xff;
+ result.add(table[b0 >> 2]);
+ result.add(table[((b0 << 4) | (b1 >> 4)) & 0x3f]);
+ result.add(table[((b1 << 2) | (b2 >> 6)) & 0x3f]);
+ result.add(table[b2 & 0x3f]);
Søren Gjesse 2012/05/30 13:40:08 You need to insert \r\n for at least every 76 char
Mads Ager (google) 2012/05/30 15:16:10 http://tools.ietf.org/html/rfc4648 explicitly say
+ }
+
+ // Deal with the last non-full block if any and add padding '='.
+ if (i == bytes.length - 1) {
+ var b0 = bytes[i] & 0xff;
+ result.add(table[b0 >> 2]);
+ result.add(table[(b0 << 4) & 0x3f]);
+ result.add('=');
+ result.add('=');
+ } else if (i == bytes.length - 2) {
+ var b0 = bytes[i] & 0xff;
+ var b1 = bytes[i + 1] & 0xff;
+ result.add(table[b0 >> 2]);
+ result.add(table[((b0 << 4) | (b1 >> 4)) & 0x3f]);
+ result.add(table[(b1 << 2) & 0x3f]);
+ result.add('=');
+ }
+
+ return result.toString();
+ }
}

Powered by Google App Engine
This is Rietveld 408576698