Skip to content

Commit 45dca5d

Browse files
committed
Adds new alphabet to Base64Codec
1 parent 5b0e140 commit 45dca5d

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.1.1
2+
3+
- Adds new alphabet to `Base64Codec`: [bcrypt][https://en.wikipedia.org/wiki/Bcrypt#base64_encoding_alphabet]
4+
15
# 2.1.0
26

37
- Adds more alphabets to `Base32Codec`. Additional alphabets are:

lib/src/codecs/base64.dart

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const _base64EncodingRfc4648 = [
2323
0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33,
2424
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f,
2525
];
26-
2726
const _base64EncodingRfc4648UrlSafe = [
2827
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, //
2928
0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
@@ -34,7 +33,6 @@ const _base64EncodingRfc4648UrlSafe = [
3433
0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33,
3534
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2d, 0x5f,
3635
];
37-
3836
const _base64DecodingRfc4648 = [
3937
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, //
4038
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
@@ -45,6 +43,26 @@ const _base64DecodingRfc4648 = [
4543
43, 44, 45, 46, 47, 48, 49, 50, 51, __, __, __, __, __, __, __, __, __, __,
4644
];
4745

46+
const _base64EncodingBcrypt = [
47+
0x2e, 0x2f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, //
48+
0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
49+
0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
50+
0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
51+
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
52+
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
53+
0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31,
54+
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
55+
];
56+
const _base64DecodingBcrypt = [
57+
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, //
58+
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
59+
__, __, __, __, __, __, __, __, 00, 01, 54, 55, 56, 57, 58, 59, 60, 61, 62,
60+
63, __, __, __, __, __, __, __, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12,
61+
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, __, __, __, __,
62+
__, __, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
63+
45, 46, 47, 48, 49, 50, 51, 52, 53, __, __, __, __, __, __, __, __, __, __,
64+
];
65+
4866
// ========================================================
4967
// Base-64 Codec
5068
// ========================================================
@@ -142,4 +160,23 @@ class Base64Codec extends HashlibCodec {
142160
alphabet: _base64DecodingRfc4648,
143161
),
144162
);
163+
164+
/// Codec instance to encode and decode 8-bit integer sequence to 6-bit
165+
/// Base-64 character sequence using the alphabet described in
166+
/// [Bcrypt](https://en.wikipedia.org/wiki/Bcrypt#base64_encoding_alphabet):
167+
/// ```
168+
/// ./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
169+
/// ```
170+
///
171+
/// It is not padded.
172+
static const Base64Codec bcrypt = Base64Codec._(
173+
encoder: AlphabetEncoder(
174+
bits: 6,
175+
alphabet: _base64EncodingBcrypt,
176+
),
177+
decoder: AlphabetDecoder(
178+
bits: 6,
179+
alphabet: _base64DecodingBcrypt,
180+
),
181+
);
145182
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: hashlib_codecs
22
description: Fast and error resilient codecs. Currently supporting Binary(Base2), Hexadecimal(Base16), Base32, Base64, BigInt.
33
homepage: https://github.com/bitanon/hashlib_codecs
4-
version: 2.1.0
4+
version: 2.1.1
55

66
environment:
77
sdk: '>=2.14.0 <4.0.0'

scripts/alphabet_maker.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ def fwd(s):
4848
# print("z-base-32 reversed", end=":")
4949
# rev(b"ybndrfg8ejkmcpqxot1uwisza345h769")
5050

51-
print("word-safe", end=":")
52-
fwd(b"23456789CFGHJMPQRVWXcfghjmpqrvwx")
53-
print("word-safe reversed", end=":")
54-
rev(b"23456789CFGHJMPQRVWXcfghjmpqrvwx")
51+
# print("word-safe", end=":")
52+
# fwd(b"23456789CFGHJMPQRVWXcfghjmpqrvwx")
53+
# print("word-safe reversed", end=":")
54+
# rev(b"23456789CFGHJMPQRVWXcfghjmpqrvwx")
55+
56+
print("base64 bcrypt", end=":")
57+
fwd(b"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
58+
print("base64 bcrypt reversed", end=":")
59+
rev(b"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")

test/base64_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ void main() {
105105
expect(fromBase64(r), equals(b), reason: 'length $i');
106106
}
107107
});
108+
test('[bcrypt] encoding <-> decoding', () {
109+
for (int i = 0; i < 100; ++i) {
110+
var b = randomBytes(i);
111+
var r = toBase64(b, codec: Base64Codec.bcrypt);
112+
var a = fromBase64(r, codec: Base64Codec.bcrypt);
113+
expect(a, equals(b), reason: 'length $i');
114+
}
115+
});
108116
group('decoding with invalid chars', () {
109117
test('Hashlib!', () {
110118
try {

0 commit comments

Comments
 (0)