Skip to content

Commit 5b0e140

Browse files
committed
Adds more alphabets to base32
1 parent de40990 commit 5b0e140

File tree

13 files changed

+552
-66
lines changed

13 files changed

+552
-66
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ test/**/*.exe
1212
# Omit committing pubspec.lock for library packages; see
1313
# https://dart.dev/guides/libraries/private-files#pubspeclock.
1414
pubspec.lock
15+
16+
# Others
17+
__pycache__/
18+
*.pyc

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# 2.1.0
2+
3+
- Adds more alphabets to `Base32Codec`. Additional alphabets are:
4+
- [base32hex](https://en.wikipedia.org/wiki/Base32#base32hex)
5+
- Lowerase base32hex
6+
- [Crockford's Base32](https://en.wikipedia.org/wiki/Base32#Crockford's_Base32)
7+
- [Geohash's Base32](https://en.wikipedia.org/wiki/Base32#Geohash)
8+
- [z-base-32](https://en.wikipedia.org/wiki/Base32#z-base-32)
9+
- [Word-safe alphabet](https://en.wikipedia.org/wiki/Base32#Word-safe_alphabet)
10+
- Allows the `padding` parameter to be effective to any codecs in `Base32Codec` and `Base64Codec`.
11+
112
# 2.0.0
213

314
- **Breaking**: Removes all constant exports.

README.md

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,34 @@ This library contains implementations of fast and error resilient codecs in pure
2020
| Class | `Base2Codec` |
2121
| Methods | `fromBinary`, `toBinary` |
2222

23+
Available codecs:
24+
25+
- **standard**: `01` (default)
26+
2327
### Octal (Base-8)
2428

2529
| Type | Available |
2630
| ------- | ---------------------- |
2731
| Class | `Base8Codec` |
2832
| Methods | `fromOctal`, `toOctal` |
2933

34+
Available codecs:
35+
36+
- **standard**: `012345678` (default)
37+
3038
### Hexadecimal (Base-16)
3139

3240
| Type | Available |
3341
| ------- | ------------------ |
3442
| Class | `Base16Codec` |
3543
| Methods | `fromHex`, `toHex` |
3644

37-
### Base-32 (RFC-4648)
45+
Available codecs:
46+
47+
- **upper**: `0123456789ABCDEF` (default)
48+
- **lower**: `0123456789abcdef`
49+
50+
### Base-32
3851

3952
> Supports conversion without padding
4053
@@ -43,7 +56,17 @@ This library contains implementations of fast and error resilient codecs in pure
4356
| Class | `Base32Codec` |
4457
| Methods | `fromBase32`, `toBase32` |
4558

46-
### Base-64 (RFC-4648)
59+
Available codecs:
60+
61+
- **standard** (RFC-4648): `ABCDEFGHIJKLMNOPQRSTUVWXYZ234567` (default)
62+
- **lowercase**: `abcdefghijklmnopqrstuvwxyz234567`
63+
- **hex**: `0123456789ABCDEFGHIJKLMNOPQRSTUV`
64+
- **hexLower**: `0123456789abcdefghijklmnopqrstuv`
65+
- **crockford**: `0123456789bcdefghjkmnpqrstuvwxyz`
66+
- **z**: `ybndrfg8ejkmcpqxot1uwisza345h769`
67+
- **wordSafe**: `23456789CFGHJMPQRVWXcfghjmpqrvwx`
68+
69+
### Base-64
4770

4871
> Supports conversion without padding, and <br>
4972
> the URL/Filename-safe Base64 conversion.
@@ -53,6 +76,11 @@ This library contains implementations of fast and error resilient codecs in pure
5376
| Class | `Base64Codec` |
5477
| Methods | `fromBase64`, `toBase64` |
5578

79+
Available codecs:
80+
81+
- **standard** (RFC-4648): `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/` (default)
82+
- **urlSafe**: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_`
83+
5684
### BigInt
5785

5886
> Supports both the Big-Endian and Little-Endian conversion
@@ -62,6 +90,11 @@ This library contains implementations of fast and error resilient codecs in pure
6290
| Class | `BigIntCodec` |
6391
| Methods | `fromBigInt`, `toBigInt` |
6492

93+
Available codecs:
94+
95+
- **msbFirst**: treats the input bytes in big-endian order
96+
- **lsbFirst**: treats the input bytes in little-endian order
97+
6598
## Getting Started
6699

67100
The following import will give you access to all of the algorithms in this package.
@@ -70,7 +103,7 @@ The following import will give you access to all of the algorithms in this packa
70103
import 'package:hashlib_codecs/hashlib_codecs.dart';
71104
```
72105

73-
Check the [API Reference](https://pub.dev/documentation/hashlib_codecs/latest/) for details.
106+
Check the [API Reference](https://pub.dev/documentation/hashlib_codecs/latest/hashlib_codecs/hashlib_codecs-library.html) for details.
74107

75108
## Usage
76109

@@ -80,26 +113,33 @@ Examples can be found inside the `example` folder.
80113
import 'package:hashlib_codecs/hashlib_codecs.dart';
81114
82115
void main() {
83-
var input = [0x3, 0xF1];
84-
print("input => $input");
116+
var inp = [0x3, 0xF1];
117+
print("input => $inp");
118+
print('');
119+
120+
print("binary => ${toBinary(inp)}");
85121
print('');
86122
87-
print("binary => ${toBinary(input)}");
88-
print("binary (no padding) => ${toBinary(input, padding: false)}");
123+
print("octal => ${toOctal(inp)}");
89124
print('');
90125
91-
print("hexadecimal => ${toHex(input)}");
92-
print("hexadecimal (uppercase) => ${toHex(input, upper: true)}");
126+
print("hexadecimal => ${toHex(inp)}");
127+
print("hexadecimal (uppercase) => ${toHex(inp, upper: true)}");
93128
print('');
94129
95-
print("base32 => ${toBase32(input)}");
96-
print("base32 (lowercase) => ${toBase32(input, lower: true)}");
97-
print("base32 (no padding) => ${toBase32(input, padding: false)}");
130+
print("base32 => ${toBase32(inp)}");
131+
print("base32 (lowercase) => ${toBase32(inp, lower: true)}");
132+
print("base32 (no padding) => ${toBase32(inp, padding: false)}");
133+
print("base32 (hex) => ${toBase32(inp, codec: Base32Codec.hex)}");
134+
print("base32 (z-base-32) => ${toBase32(inp, codec: Base32Codec.z)}");
135+
print("base32 (geohash) => ${toBase32(inp, codec: Base32Codec.geohash)}");
136+
print("base32 (crockford) => ${toBase32(inp, codec: Base32Codec.crockford)}");
137+
print("base32 (word-safe) => ${toBase32(inp, codec: Base32Codec.wordSafe)}");
98138
print('');
99139
100-
print("base64 => ${toBase64(input)}");
101-
print("base64url => ${toBase64(input, url: true)}");
102-
print("base64 (no padding) => ${toBase64(input, padding: false)}");
140+
print("base64 => ${toBase64(inp)}");
141+
print("base64url => ${toBase64(inp, url: true)}");
142+
print("base64 (no padding) => ${toBase64(inp, padding: false)}");
103143
print('');
104144
}
105145
```
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
import 'package:hashlib_codecs/hashlib_codecs.dart';
22

33
void main() {
4-
var input = [0x3, 0xF1];
5-
print("input => $input");
4+
var inp = [0x3, 0xF1];
5+
print("input => $inp");
66
print('');
77

8-
print("binary => ${toBinary(input)}");
8+
print("binary => ${toBinary(inp)}");
99
print('');
1010

11-
print("octal => ${toOctal(input)}");
11+
print("octal => ${toOctal(inp)}");
1212
print('');
1313

14-
print("hexadecimal => ${toHex(input)}");
15-
print("hexadecimal (uppercase) => ${toHex(input, upper: true)}");
14+
print("hexadecimal => ${toHex(inp)}");
15+
print("hexadecimal (uppercase) => ${toHex(inp, upper: true)}");
1616
print('');
1717

18-
print("base32 => ${toBase32(input)}");
19-
print("base32 (lowercase) => ${toBase32(input, lower: true)}");
20-
print("base32 (no padding) => ${toBase32(input, padding: false)}");
18+
print("base32 => ${toBase32(inp)}");
19+
print("base32 (lowercase) => ${toBase32(inp, lower: true)}");
20+
print("base32 (no padding) => ${toBase32(inp, padding: false)}");
21+
print("base32 (hex) => ${toBase32(inp, codec: Base32Codec.hex)}");
22+
print("base32 (z-base-32) => ${toBase32(inp, codec: Base32Codec.z)}");
23+
print("base32 (geohash) => ${toBase32(inp, codec: Base32Codec.geohash)}");
24+
print("base32 (crockford) => ${toBase32(inp, codec: Base32Codec.crockford)}");
25+
print("base32 (word-safe) => ${toBase32(inp, codec: Base32Codec.wordSafe)}");
2126
print('');
2227

23-
print("base64 => ${toBase64(input)}");
24-
print("base64url => ${toBase64(input, url: true)}");
25-
print("base64 (no padding) => ${toBase64(input, padding: false)}");
28+
print("base64 => ${toBase64(inp)}");
29+
print("base64url => ${toBase64(inp, url: true)}");
30+
print("base64 (no padding) => ${toBase64(inp, padding: false)}");
2631
print('');
2732
}

lib/src/base32.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ Base32Codec _codecFromParameters({
2020
}
2121
}
2222

23+
const _codecsWithPadding = {
24+
Base32Codec.standard,
25+
Base32Codec.lowercase,
26+
Base32Codec.hex,
27+
Base32Codec.hexLower,
28+
Base32Codec.geohash,
29+
Base32Codec.wordSafe,
30+
};
31+
2332
/// Converts 8-bit integer sequence to 5-bit Base-32 character sequence.
2433
///
2534
/// Parameters:
2635
/// - [input] is a sequence of 8-bit integers
27-
/// - If [lower] is true, the lowercase standard alphabet is used.
36+
/// - If [lower] is true, the [Base32Codec.lowercase] alphabet is used.
2837
/// - If [padding] is true, the output will not have padding characters.
2938
/// - [codec] is the [Base32Codec] to use. It is derived from the other
3039
/// parameters if not provided.
@@ -39,6 +48,9 @@ String toBase32(
3948
padding: padding,
4049
);
4150
var out = codec.encoder.convert(input);
51+
if (!padding && _codecsWithPadding.contains(codec)) {
52+
out = out.takeWhile((x) => x != codec!.encoder.padding);
53+
}
4254
return String.fromCharCodes(out);
4355
}
4456

lib/src/base64.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ Base64Codec _codecFromParameters({
2020
}
2121
}
2222

23+
const _codecsWithPadding = {
24+
Base64Codec.standard,
25+
Base64Codec.urlSafe,
26+
};
27+
2328
/// Converts 8-bit integer sequence to 6-bit Base-64 character sequence.
2429
///
2530
/// Parameters:
@@ -39,6 +44,9 @@ String toBase64(
3944
padding: padding,
4045
);
4146
var out = codec.encoder.convert(input);
47+
if (!padding && _codecsWithPadding.contains(codec)) {
48+
out = out.takeWhile((x) => x != codec!.encoder.padding);
49+
}
4250
return String.fromCharCodes(out);
4351
}
4452

0 commit comments

Comments
 (0)