Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@scure/base
Advanced tools
Secure, audited & 0-dep implementation of bech32, base64, base58, base32 & base16
@scure/base is a JavaScript library that provides utilities for encoding and decoding data in various formats such as Base64, Base58, and Base16. It is designed to be lightweight and efficient, making it suitable for use in both browser and Node.js environments.
Base64 Encoding and Decoding
This feature allows you to encode and decode strings to and from Base64 format. It is useful for data serialization and transmission.
const { base64 } = require('@scure/base');
// Encoding a string to Base64
const encoded = base64.encode('Hello, World!');
console.log(encoded); // Outputs: 'SGVsbG8sIFdvcmxkIQ=='
// Decoding a Base64 string
const decoded = base64.decode(encoded);
console.log(decoded); // Outputs: 'Hello, World!'
Base58 Encoding and Decoding
This feature allows you to encode and decode strings to and from Base58 format. Base58 is commonly used in cryptocurrencies for encoding addresses and keys.
const { base58 } = require('@scure/base');
// Encoding a string to Base58
const encoded = base58.encode('Hello, World!');
console.log(encoded); // Outputs: '72k1xXWG59wUsYvG'
// Decoding a Base58 string
const decoded = base58.decode(encoded);
console.log(decoded); // Outputs: 'Hello, World!'
Base16 Encoding and Decoding
This feature allows you to encode and decode strings to and from Base16 (Hexadecimal) format. It is useful for representing binary data in a human-readable form.
const { base16 } = require('@scure/base');
// Encoding a string to Base16 (Hex)
const encoded = base16.encode('Hello, World!');
console.log(encoded); // Outputs: '48656c6c6f2c20576f726c6421'
// Decoding a Base16 (Hex) string
const decoded = base16.decode(encoded);
console.log(decoded); // Outputs: 'Hello, World!'
base-x is a library for encoding and decoding data in various base formats, including Base58. It is similar to @scure/base in terms of functionality but focuses more on Base58 and other custom base encodings.
bs58 is a library specifically for Base58 encoding and decoding. It is highly optimized for performance and is commonly used in cryptocurrency applications. Unlike @scure/base, it does not support other base formats like Base64 or Base16.
base64-js is a library for Base64 encoding and decoding. It is lightweight and efficient, similar to @scure/base, but it only supports Base64 encoding and decoding.
Secure, audited and minimal implementation of bech32, base64, base58, base32 & base16.
Written in functional style, uses chaining, has unique tests which ensure correctness.
Matches following specs:
The library has been audited by Cure53 on Jan 5, 2022. Check out the audit PDF & URL. Before the audit, it was called micro-base
.
scure — secure audited packages for every use case.
npm install @scure/base
Or
yarn add @scure/base
const {base16, base32, base64, base58} = require('@scure/base');
// Flavors
const {base58xmr, base58xrp, base32hex, base32crockford, base64url} = require('@scure/base');
const data = Uint8Array.from([1, 2, 3]);
base64.decode(base64.encode(data));
// Everything has the same API except for bech32 and base58check
base32.encode(data);
base16.encode(data);
base32hex.encode(data);
// bech32
const {bech32, bech32m} = require('@scure/base');
const words = bech32.toWords(data);
const be = bech32.encode('prefix', words);
const {prefix, words} = bech32.decode(be);
bech32m.encode('prefix', words);
// base58check is special-case
// you need to pass sha256() function that returns Uint8Array
const {base58check} = require('@scure/base');
base58check(sha256).encode(data);
// Alternative API
const {str, bytes} = require('@scure/base');
const encoded = str('base64', data);
const data = bytes('base64', encoded);
The code may feel unnecessarily complicated; but actually it's much easier to reason about. Any encoding library consists of two functions:
encode(A) -> B
decode(B) -> A
where X = decode(encode(X))
# encode(decode(X)) can be !== X!
# because decoding can normalize input
e.g.
base58checksum = {
encode(): {
// checksum
// radix conversion
// alphabet
},
decode(): {
// alphabet
// radix conversion
// checksum
}
}
But instead of creating two big functions for each specific case, we create them from tiny composamble building blocks:
base58checksum = chain(checksum(), radix(), alphabet())
Which is the same as chain/pipe/sequence function in Functional Programming, but significantly more useful since it enforces same order of execution of encode/decode. Basically you only define encode (in declarative way) and get correct decode for free. So, instead of reasoning about two big functions you need only reason about primitives and encode chain. The design revealed obvious bug in older version of the lib, where xmr version of base58 had errors in decode's block processing.
Besides base-encodings, we can reuse the same approach with any encode/decode function
(bytes2number
, bytes2u32
, etc).
For example, you can easily encode entropy to mnemonic (BIP-39):
export function getCoder(wordlist: string[]) {
if (!Array.isArray(wordlist) || wordlist.length !== 2 ** 11 || typeof wordlist[0] !== 'string') {
throw new Error('Worlist: expected array of 2048 strings');
}
return mbc.chain(mbu.checksum(1, checksum), mbu.radix2(11, true), mbu.alphabet(wordlist));
}
Uint8Array
is represented as big-endian number:
[1, 2, 3, 4, 5] -> 1*(256**4) + 2*(256**3) 3*(256**2) + 4*(256**1) + 5*(256**0)
where 256 = 2**8 (8 bits per byte)
which is then converted to a number in another radix/base (16/32/58/64, etc).
However, generic conversion between bases has quadratic O(n^2) time complexity.
Which means base58 has quadratic time complexity too. Use base58 only when you have small constant sized input, because variable length sized input from user can cause DoS.
On the other hand, if both bases are power of same number (like 2**8 <-> 2**64
),
there is linear algorithm. For now we have implementation for power-of-two bases only (radix2).
MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.
FAQs
Secure, audited & 0-dep implementation of base64, bech32, base58, base32 & base16
The npm package @scure/base receives a total of 808,788 weekly downloads. As such, @scure/base popularity was classified as popular.
We found that @scure/base demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.