crypto

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

README

pg/crypto

The pgcrypto extension for sqlb. Needs CREATE EXTENSION pgcrypto.

SQL Helper
crypt, gen_salt Crypt, GenSalt
digest, hmac Digest, HMAC
pgp_sym_encrypt, pgp_sym_decrypt PgpSymEncrypt, PgpSymDecrypt
gen_random_bytes GenRandomBytes

gen_random_uuid is core Postgres since 13 and lives in pg as GenRandomUUID. encode(..., 'hex') is core too: pg.Encode.

Hashing on the server puts the plain password on the wire and in log_statement. Hash in Go where you can; these helpers exist for schemas that already store pgcrypto hashes:

ins.Row(sqlb.Set(UserEmail, email), sqlb.SetExpr(UserPass, crypto.Crypt(sqlb.Param(pw), crypto.GenSalt("bf", 12))))
sel.Where(sqlb.EqExpr(crypto.Crypt(sqlb.Param(pw), UserPass), UserPass))

Documentation

Overview

Package crypto is the pgcrypto extension: hashing, password hashing and symmetric encryption on the server. Needs CREATE EXTENSION pgcrypto.

Hashing passwords on the server sends the plain password over the wire and into the statement log. Prefer hashing in Go and storing the hash; these helpers exist for schemas that already commit to pgcrypto.

Index

Constants

This section is empty.

Variables

View Source
var Extension = pg.Extension{Name: "pgcrypto"}

Extension is the pgcrypto extension this package's constructs need, for pg.CreateExtension(Extension), so a library installs what it uses without spelling its name.

Functions

func Crypt

func Crypt(password, salt sqlb.Expr[string]) sqlb.Expr[string]

Crypt renders crypt(password, salt). To verify: crypt(input, stored) = stored.

func Digest

func Digest(data sqlb.Expr[string], algo string) sqlb.Expr[[]byte]

Digest renders digest(data, algo); algo is "sha256", "sha1", "md5".

func GenRandomBytes

func GenRandomBytes(n int) sqlb.Expr[[]byte]

GenRandomBytes renders gen_random_bytes(n).

func GenSalt

func GenSalt(typ string, rounds int) sqlb.Expr[string]

GenSalt renders gen_salt(type, rounds); type is "bf", "md5", "xdes" or "des" and rounds is the bcrypt cost.

func HMAC

func HMAC(data, key sqlb.Expr[string], algo string) sqlb.Expr[[]byte]

HMAC renders hmac(data, key, algo).

func PgpSymDecrypt

func PgpSymDecrypt(data sqlb.Expr[[]byte], key sqlb.Expr[string]) sqlb.Expr[string]

PgpSymDecrypt renders pgp_sym_decrypt(data, key), the reverse of PgpSymEncrypt. It returns text, so the ciphertext column is []byte and the result is a string.

func PgpSymEncrypt

func PgpSymEncrypt(data, key sqlb.Expr[string]) sqlb.Expr[[]byte]

PgpSymEncrypt renders pgp_sym_encrypt(data, key). The key travels to the server on every call, so the server and its logs see it.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL