npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@alessiofrittoli/crypto-encoder

v1.1.0

Published

Lightweight TypeScript encoder/decoder library

Downloads

232

Readme

Crypto Encoder 🧮

Version 1.1.0

Lightweight TypeScript encoder/decoder library

Table of Contents


Getting started

Run the following command to start using crypto-encoder in your projects:

npm i @alessiofrittoli/crypto-encoder

or using pnpm

pnpm i @alessiofrittoli/crypto-encoder

Base32

This module provides a Base32 utility class for encoding and decoding data according to various Base32 specifications.
It supports multiple Base32 variants and offers flexible options for encoding.

The Base32 class provides methods to encode and decode data using Base32, supporting multiple variants as defined by the following specifications:

The following Base32 variants are supported:

  • RFC3548 - Alias for RFC4648
  • RFC4648 - The standard Base32 encoding.
  • RFC4648-HEX - Base32 encoding with a hexadecimal-like alphabet.
  • Crockford - A Base32 variant designed to be human-friendly.

Static Properties

Base32.VARIANT

An object containing the available Base32 variants:

Base32.VARIANT = {
	RFC3548		: 'RFC3548',
	RFC4648		: 'RFC4648',
	RFC4648_HEX	: 'RFC4648-HEX',
	Crockford	: 'Crockford',
}

Static Methods

Base32.encode()

Encodes data to a Base32 string.

Parameters

| Parameter | Type | Description | |-------------------|-----------------|------------------------------------| | data | string \| number[] \| ArrayLike<number> \| ArrayBuffer \| Int8Array \| Int16Array \| Int32Array \| Uint8Array \| Uint16Array \| Uint32Array \| Uint8ClampedArray | The data to encode. | | variant | Variant | The Base32 variant to use. | | options | EncodeOptions | (Optional) Encoding options. | | options.padding | boolean | If set, forcefully enable or disable padding. The default behavior is to follow the default of the selected variant. |

Returns

Type: string

A Base32 encoded string.

Example usage
import { Base32 } from '@alessiofrittoli/crypto-encoder'
// or
import Base32 from '@alessiofrittoli/crypto-encoder/Base32'

console.log( Base32.encode( 'some value', 'RFC3548' ) )
// or
console.log( Base32.encode( 'some value', Base32.VARIANT.RFC3548 ) )
// Outputs: 'ONXW2ZJAOZQWY5LF'

Base32.decode()

Decodes a Base32 string to binary data.

Parameters

| Parameter | Type | Description | |-------------------|-----------------|----------------------------------------------| | input | string | The Base32-encoded string. | | variant | Variant | The Base32 variant used to encode the input. |

Returns

Type: ArrayBuffer

An ArrayBuffer containing the decoded data.

Example usage
import { Base32 } from '@alessiofrittoli/crypto-encoder'
// or
import Base32 from '@alessiofrittoli/crypto-encoder/Base32'

const input		= 'ONXW2ZJAOZQWY5LF'
const decoded	= Base32.decode( input, 'RFC3548' )
// or
const decoded	= Base32.decode( input, Base32.VARIANT.RFC3548 )

console.log( Buffer.from( decoded ).toString() ) // Node.js
// or
console.log( new TextDecoder().decode( decoded ) ) // client-side
// or
console.log( Base32.toString( decoded ) ) // Node.js + client-side
// Outputs: 'some value'

Base64

This module provides a static Base64 utility class for encoding and decoding data to and from Base64/Base64url formats. It supports various input types and offers options for normalization between Base64 and Base64url formats.

The Base64 class provides static methods for:

  • Encoding data to Base64/Base64url strings.
  • Decoding Base64/Base64url strings to binary data.
  • Normalizing strings between Base64 and Base64url formats.

The implementation is compatible with both browser and Node.js environments.

Static Methods

Base64.encode()

Encodes data to a Base64 or Base64url string.

Parameters

| Parameter | Type | Default | Description | |-----------|-----------------|---------| ----------------------------------------------| | input | string \| number[] \| Buffer \| ArrayBuffer \| Int8Array \| Int16Array \| Int32Array \| Uint8Array \| Uint16Array \| Uint32Array \| Uint8ClampedArray | - | The data to encode. | | normalize | boolean | false | Whether to normalize the output to Base64url. |

Returns

Type: string

A Base64 or Base64url encoded string.

Example
const data		= 'Hello, World!'
const base64	= Base64.encode( data )
const base64url	= Base64.encode( data, true )

console.log( base64 ) // Outputs: 'SGVsbG8sIFdvcmxkIQ=='
console.log( base64url ) // Outputs: 'SGVsbG8sIFdvcmxkIQ'

Base64.decode()

Decodes a Base64 or Base64url string.

Parameters

| Parameter | Type | Description | |-----------|----------|-------------------------------------------| | data | string | The Base64 or Base64url string to decode. |

Returns

Type: Buffer

A Buffer containing the decoded data.

Example
const base64	= 'SGVsbG8sIFdvcmxkIQ=='
const base64url	= 'SGVsbG8sIFdvcmxkIQ'

console.log( Base64.toString( Base64.decode( base64 ) ) )
// or
console.log( Base64.toString( Base64.decode( base64url ) ) )
// Outputs: 'Hello, World!'

Security

If you believe you have found a security vulnerability, we encourage you to responsibly disclose this and NOT open a public issue. We will investigate all legitimate reports. Email [email protected] to disclose any security vulnerabilities.

Made with ☕