
Company News
Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development
Socket is scaling to defend open source against supply chain attacks as AI accelerates software development.
@pgpm/base32
Advanced tools
RFC4648 Base32 encode/decode in plpgsql
@pgpm/base32 implements Base32 encoding and decoding entirely in PostgreSQL using plpgsql. Base32 is commonly used for encoding binary data in a human-readable format, particularly for TOTP secrets, API keys, and other security tokens. This package provides a pure SQL implementation without external dependencies.
If you have pgpm installed:
pgpm install @pgpm/base32
pgpm deploy
This is a quick way to get started. The sections below provide more detailed installation options.
# Install pgpm CLI
npm install -g pgpm
# Start local Postgres (via Docker) and export env vars
pgpm docker start
eval "$(pgpm env)"
Tip: Already running Postgres? Skip the Docker step and just export your
PG*environment variables.
# 1. Install the package
pgpm install @pgpm/base32
# 2. Deploy locally
pgpm deploy
# 1. Create a workspace
pgpm init workspace
# 2. Create your first module
cd my-workspace
pgpm init
# 3. Install a package
cd packages/my-module
pgpm install @pgpm/base32
# 4. Deploy everything
pgpm deploy --createdb --database mydb1
select base32.encode('foo');
-- MZXW6===
select base32.decode('MZXW6===');
-- foo
Base32 is the standard encoding for TOTP secrets:
-- Generate a random secret and encode it
SELECT base32.encode('randomsecret123');
-- Result: MJQXGZJTGIQGS4ZAON2XAZLSEBRW63LNN5XCA2LOEBRW63LQMFZXG===
-- Use with TOTP
SELECT totp.generate(base32.encode('mysecret'));
Encode binary data as human-readable API keys:
-- Encode a UUID as Base32
SELECT base32.encode(gen_random_uuid()::text);
-- Create a table with Base32-encoded keys
CREATE TABLE api_keys (
id serial PRIMARY KEY,
user_id uuid,
key_encoded text DEFAULT base32.encode(gen_random_bytes(20)::text),
created_at timestamptz DEFAULT now()
);
Encode sensitive identifiers:
-- Encode user IDs for public URLs
CREATE FUNCTION get_public_user_id(user_uuid uuid)
RETURNS text AS $$
BEGIN
RETURN base32.encode(user_uuid::text);
END;
$$ LANGUAGE plpgsql;
-- Decode back to UUID
CREATE FUNCTION get_user_from_public_id(public_id text)
RETURNS uuid AS $$
BEGIN
RETURN base32.decode(public_id)::uuid;
END;
$$ LANGUAGE plpgsql;
Encode checksums and hashes:
-- Encode a SHA256 hash
SELECT base32.encode(
encode(digest('file contents', 'sha256'), 'hex')
);
Base32 is essential for TOTP authentication:
-- Store TOTP secret in Base32 format
CREATE TABLE user_2fa (
user_id uuid PRIMARY KEY,
secret_base32 text NOT NULL,
enabled boolean DEFAULT false
);
-- Generate and store Base32-encoded secret
INSERT INTO user_2fa (user_id, secret_base32)
VALUES (
'user-uuid',
base32.encode('randomsecret')
);
-- Generate TOTP code from Base32 secret
SELECT totp.generate(
base32.decode(secret_base32)
) FROM user_2fa WHERE user_id = 'user-uuid';
Combine with encrypted secrets for secure storage:
-- Store Base32-encoded secret encrypted
SELECT encrypted_secrets.secrets_upsert(
'user-uuid',
'totp_secret',
base32.encode('mysecret'),
'pgp'
);
-- Retrieve and use
SELECT totp.generate(
base32.decode(
encrypted_secrets.secrets_getter('user-uuid', 'totp_secret')
)
);
Base32 uses the following character set (RFC 4648):
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 2 3 4 5 6 7
Padding character: =
Base32 vs Base64:
| Feature | Base32 | Base64 |
|---|---|---|
| Character Set | A-Z, 2-7 | A-Z, a-z, 0-9, +, / |
| Case Sensitive | No | Yes |
| URL Safe | Yes | Requires modification |
| Human Readable | More readable | Less readable |
| Efficiency | ~60% overhead | ~33% overhead |
| Use Case | TOTP, user-facing | General encoding |
Base32 is preferred for TOTP because:
pnpm test
None - this is a pure plpgsql implementation.
Thanks to
https://tools.ietf.org/html/rfc4648
https://www.youtube.com/watch?v=Va8FLD-iuTg
libpg_query, converting SQL into parse trees.AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
FAQs
Base32 encoding and decoding functions for PostgreSQL
The npm package @pgpm/base32 receives a total of 1,037 weekly downloads. As such, @pgpm/base32 popularity was classified as popular.
We found that @pgpm/base32 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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.

Company News
Socket is scaling to defend open source against supply chain attacks as AI accelerates software development.

Research
/Security News
A long-running Go typosquat impersonated the popular shopspring/decimal library and used DNS TXT records to execute commands.

Research
Active npm supply chain attack compromises @antv packages in a fast-moving malicious publish wave tied to Mini Shai-Hulud.