Skip to content

Commit

Permalink
Merge pull request #34 from benjreinhart/base32nopad
Browse files Browse the repository at this point in the history
Add base32nopad and base32hexnopad
  • Loading branch information
paulmillr authored Jun 13, 2024
2 parents 66cd490 + 44a2b5d commit 72c6ec6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ import { base16, base32, base64, base58 } from '@scure/base';
import {
base58xmr,
base58xrp,
base32nopad,
base32hex,
base32hexnopad,
base32crockford,
base64nopad,
base64url,
Expand Down
10 changes: 10 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,22 @@ export const base32: BytesCoder = /* @__PURE__ */ chain(
padding(5),
join('')
);
export const base32nopad: BytesCoder = /* @__PURE__ */ chain(
radix2(5),
alphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'),
join('')
);
export const base32hex: BytesCoder = /* @__PURE__ */ chain(
radix2(5),
alphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV'),
padding(5),
join('')
);
export const base32hexnopad: BytesCoder = /* @__PURE__ */ chain(
radix2(5),
alphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV'),
join('')
);
export const base32crockford: BytesCoder = /* @__PURE__ */ chain(
radix2(5),
alphabet('0123456789ABCDEFGHJKMNPQRSTVWXYZ'),
Expand Down
55 changes: 54 additions & 1 deletion test/bases.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
const assert = require('assert');
const {
base32,
base32nopad,
base32hex,
base32hexnopad,
base32crockford,
base58,
base58xmr,
base58check: _base58check,
base58xrp,
base64,
base64nopad,
base64url,
base64urlnopad,
bech32,
bech32m,
bytes,
str,
utils,
} = require('..');
const base58check = _base58check((buf) =>
Uint8Array.from(require('crypto').createHash('sha256').update(buf).digest())
);
const { base32, base32hex, base32crockford, base64, base64url, str, bytes } = require('..');
const { Buffer } = require('buffer');
const { should } = require('micro-should');
const { RANDOM } = require('./utils');
Expand Down Expand Up @@ -76,6 +86,49 @@ should('14335 vectors, base32/64 58/hex/url/xmr, bech32/m', () => {
assert.deepStrictEqual(coder[v.fn_name].decode(v.exp), data, 'decode ' + i);
}
});

const TEST_BYTES = new TextEncoder().encode('@scure/base encoding / decoding');

should('nopad variants: base32', () => {
assert.strictEqual(
base32nopad.encode(TEST_BYTES),
'IBZWG5LSMUXWEYLTMUQGK3TDN5SGS3THEAXSAZDFMNXWI2LOM4'
);

assert.deepStrictEqual(
base32nopad.decode('IBZWG5LSMUXWEYLTMUQGK3TDN5SGS3THEAXSAZDFMNXWI2LOM4'),
TEST_BYTES
);

assert.strictEqual(
base32hexnopad.encode(TEST_BYTES),
'81PM6TBICKNM4OBJCKG6ARJ3DTI6IRJ740NI0P35CDNM8QBECS'
);

assert.deepStrictEqual(
base32hexnopad.decode('81PM6TBICKNM4OBJCKG6ARJ3DTI6IRJ740NI0P35CDNM8QBECS'),
TEST_BYTES
);
});

should('nopad variants: base64', () => {
assert.strictEqual(base64nopad.encode(TEST_BYTES), 'QHNjdXJlL2Jhc2UgZW5jb2RpbmcgLyBkZWNvZGluZw');
assert.deepStrictEqual(
base64nopad.decode('QHNjdXJlL2Jhc2UgZW5jb2RpbmcgLyBkZWNvZGluZw'),
TEST_BYTES
);

assert.strictEqual(
base64urlnopad.encode(TEST_BYTES),
'QHNjdXJlL2Jhc2UgZW5jb2RpbmcgLyBkZWNvZGluZw'
);

assert.deepStrictEqual(
base64urlnopad.decode('QHNjdXJlL2Jhc2UgZW5jb2RpbmcgLyBkZWNvZGluZw'),
TEST_BYTES
);
});

should('utils: radix2', () => {
const t = (bits) => {
const coder = utils.radix2(bits);
Expand Down

0 comments on commit 72c6ec6

Please sign in to comment.