add base 58
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2020-12-30 15:42:08 +01:00
parent fcc7fd79d4
commit 2e9c3896a5
7 changed files with 143 additions and 9 deletions

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## 1.1.0
added support for base 58
## 1.0.0 ## 1.0.0
initial release initial release

View File

@ -1,6 +1,6 @@
# @sapphirecode/encoding-helper # @sapphirecode/encoding-helper
version: 1.0.x version: 1.1.x
Converting strings between different encodings Converting strings between different encodings
@ -21,11 +21,26 @@ const encoding = require('@sapphirecode/encoding-helper');
const hex = encoding.to_hex('abc'); // convert any encoding to hex, default is utf-8 const hex = encoding.to_hex('abc'); // convert any encoding to hex, default is utf-8
const b64 = encoding.to_b64(hex, 'hex'); // convert any encoding to base64, default is utf-8 const b64 = encoding.to_b64(hex, 'hex'); // convert any encoding to base64, default is utf-8
const b58 = encoding.to_b58(hex, 'hex'); // convert any encoding to base58, default is utf-8
const utf8 = encoding.to_utf8(hex, 'hex'); // convert any encoding to utf-8, no default const utf8 = encoding.to_utf8(hex, 'hex'); // convert any encoding to utf-8, no default
const hexn = encoding.num_to_hex(10); // convert a number to hex const hexn = encoding.num_to_hex(10); // convert a number to hex
const num = encoding.hex_to_num('a'); // convert hex to a number const num = encoding.hex_to_num('a'); // convert hex to a number
``` ```
supported input encodings:
- default supported by buffer:
- ascii
- base64
- binary
- hex
- latin1
- ucs-2
- utf-8
- utf16le
- additional encodings:
- base58
## License ## License
MIT © Timo Hocker <timo@scode.ovh> MIT © Timo Hocker <timo@scode.ovh>

View File

@ -6,10 +6,18 @@
*/ */
// @ts-nocheck
/* eslint-disable no-magic-numbers */ /* eslint-disable no-magic-numbers */
'use strict'; 'use strict';
const b58 = require ('b58');
function get_buffer (str, encoding) {
if (encoding === 'base58')
return b58.decode (str);
return Buffer.from (str, encoding);
}
/** /**
* encode a string to base64 * encode a string to base64
* *
@ -18,10 +26,22 @@
* @returns {string} base64 * @returns {string} base64
*/ */
function to_b64 (str, encoding = '') { function to_b64 (str, encoding = '') {
const buf = Buffer.from (str, encoding); const buf = get_buffer (str, encoding);
return buf.toString ('base64'); return buf.toString ('base64');
} }
/**
* encode a string to base58
*
* @param {string} str string to encode
* @param {string} encoding encoding the string is in
* @returns {string} base58
*/
function to_b58 (str, encoding = '') {
const buf = get_buffer (str, encoding);
return b58.encode (buf);
}
/** /**
* encode a string to utf-8 * encode a string to utf-8
* *
@ -30,7 +50,7 @@ function to_b64 (str, encoding = '') {
* @returns {string} utf-8 * @returns {string} utf-8
*/ */
function to_utf8 (str, encoding) { function to_utf8 (str, encoding) {
const buf = Buffer.from (str, encoding); const buf = get_buffer (str, encoding);
return buf.toString ('utf-8'); return buf.toString ('utf-8');
} }
@ -42,7 +62,7 @@ function to_utf8 (str, encoding) {
* @returns {string} hex * @returns {string} hex
*/ */
function to_hex (str, encoding = '') { function to_hex (str, encoding = '') {
const buf = Buffer.from (str, encoding); const buf = get_buffer (str, encoding);
return buf.toString ('hex'); return buf.toString ('hex');
} }
@ -72,9 +92,9 @@ function hex_to_num (h) {
module.exports = { module.exports = {
to_b64, to_b64,
to_b58,
to_hex, to_hex,
to_utf8, to_utf8,
num_to_hex, num_to_hex,
hex_to_num hex_to_num
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "@sapphirecode/encoding-helper", "name": "@sapphirecode/encoding-helper",
"version": "1.0.51", "version": "1.1.0",
"main": "index.js", "main": "index.js",
"author": { "author": {
"name": "Timo Hocker", "name": "Timo Hocker",
@ -35,5 +35,8 @@
"LICENSE", "LICENSE",
"index.js", "index.js",
"index.d.ts" "index.d.ts"
] ],
"dependencies": {
"b58": "^4.0.3"
}
} }

62
test/spec/b58.js Normal file
View File

@ -0,0 +1,62 @@
'use strict';
const b58 = require ('b58');
// eslint-disable-next-line max-lines-per-function
describe ('b58', () => {
it ('should encode "Hello World!"', () => {
const enc = b58.encode (Buffer.from ('Hello World!', 'utf-8'));
expect (enc)
.toEqual ('2NEpo7TZRRrLZSi2U');
});
it ('should encode "The quick brown fox.."', () => {
const enc = b58.encode (Buffer.from (
'The quick brown fox jumps over the lazy dog.',
'utf-8'
));
expect (enc)
.toEqual ('USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z');
});
it ('should encode test hex string', () => {
const enc = b58.encode (Buffer.from ('0000287fb4cd', 'hex'));
expect (enc)
.toEqual ('11233QC4');
});
it ('should decode "Hello World!"', () => {
const dec = b58.decode ('2NEpo7TZRRrLZSi2U')
.toString ('utf-8');
expect (dec)
.toEqual ('Hello World!');
});
it ('should decode "The quick brown fox.."', () => {
const dec = b58.decode (
'USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z'
)
.toString ('utf-8');
expect (dec)
.toEqual ('The quick brown fox jumps over the lazy dog.');
});
it ('should decode test hex string', () => {
const dec = b58.decode ('11233QC4')
.toString ('hex');
expect (dec)
.toEqual ('0000287fb4cd');
});
it ('should encode any byte combination', () => {
for (let i = 0; i < 65536; i++) {
const buf = Buffer.alloc (2);
buf.writeUInt16BE (i);
const enc = b58.encode (buf);
const dec = b58.decode (enc)
.readUInt16BE ();
expect (dec)
.toEqual (i);
}
});
});

View File

@ -24,12 +24,24 @@ describe ('encoding', () => {
.toEqual ('Zm9v'); .toEqual ('Zm9v');
}); });
it ('encoding to_b58', () => {
const hex = encoding.to_b58 ('foo');
expect (hex)
.toEqual ('bQbp');
});
it ('encoding to_utf8', () => { it ('encoding to_utf8', () => {
const utf = encoding.to_utf8 ('Zm9v', 'base64'); const utf = encoding.to_utf8 ('Zm9v', 'base64');
expect (utf) expect (utf)
.toEqual ('foo'); .toEqual ('foo');
}); });
it ('encoding to_utf8 from b58', () => {
const utf = encoding.to_utf8 ('bQbp', 'base58');
expect (utf)
.toEqual ('foo');
});
it ('convert num to hex', () => { it ('convert num to hex', () => {
const res = encoding.num_to_hex (11); const res = encoding.num_to_hex (11);
expect (res) expect (res)
@ -54,4 +66,3 @@ describe ('encoding', () => {
.toEqual (11); .toEqual (11);
}); });
}); });

View File

@ -489,11 +489,25 @@ atob@^2.1.2:
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg== integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==
b58@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/b58/-/b58-4.0.3.tgz#852a28d804b4a5a60685f9b51706315145a48c36"
integrity sha512-VDtdiomm0ywbL8YzgevOZ9pcx6LuOZ3d9qYTPDcYUPf7dRYNA8wvK6epYy0FKMWIM5uaDwd3kWt1x+1S9scB1Q==
dependencies:
base-x "^3.0.2"
balanced-match@^1.0.0: balanced-match@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c= integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=
base-x@^3.0.2:
version "3.0.8"
resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.8.tgz#1e1106c2537f0162e8b52474a557ebb09000018d"
integrity sha512-Rl/1AWP4J/zRrk54hhlxH4drNxPJXYUaKffODVI53/dAsV4t9fBxyxYKAVPU1XBHxYwOWP9h9H0hM2MVw4YfJA==
dependencies:
safe-buffer "^5.0.1"
base@^0.11.1: base@^0.11.1:
version "0.11.2" version "0.11.2"
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f" resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
@ -2148,6 +2162,11 @@ rxjs@^6.6.0, rxjs@~6.6.0:
dependencies: dependencies:
tslib "^1.9.0" tslib "^1.9.0"
safe-buffer@^5.0.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
safe-buffer@~5.1.1: safe-buffer@~5.1.1:
version "5.1.2" version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"