Skip to content

Hash

The @std/hash module provides hashing and password hashing helpers.

import hash from "@std/hash"
println(hash.sha256("hello"))

Most hash functions accept either a string or []byte.

let input: string | []byte
NameReturnsDescription
md5(value)stringMD5 hash as hex.
sha1(value)stringSHA-1 hash as hex.
sha256(value)stringSHA-256 hash as hex.
sha512(value)stringSHA-512 hash as hex.
blake3(value)stringBLAKE3 hash as hex.
bcrypt(password, cost)stringBcrypt password hash.
bcrypt.compare(password, hash)boolCompares a password with a bcrypt hash.
argon2(password, salt)stringArgon2id hash encoded as base64.
import hash from "@std/hash"
println(hash.md5("hello"))
println(hash.sha1("hello"))
println(hash.sha256("hello"))
println(hash.sha512("hello"))
println(hash.blake3("hello"))

These functions also accept byte lists.

import hash from "@std/hash"
let data = bytes("hello")
println(hash.sha256(data))

Creates a bcrypt password hash.

Arguments:

NameTypeDefaultDescription
password`string[]byte`none
costintbcrypt defaultBcrypt cost.

Returns: string

import hash from "@std/hash"
let hashed = hash.bcrypt("secret")
println(hashed)

With a custom cost:

import hash from "@std/hash"
let hashed = hash.bcrypt("secret", 12)
println(hashed)

Compares a password with a bcrypt hash.

Arguments:

NameTypeDescription
password`string[]byte`
hash`string[]byte`

Returns: bool

import hash from "@std/hash"
let hashed = hash.bcrypt("secret")
if hash.bcrypt.compare("secret", hashed) {
println("password ok")
}

Creates an Argon2id hash and returns it as base64.

Arguments:

NameTypeDescription
password`string[]byte`
salt`string[]byte`

Returns: string

import hash from "@std/hash"
let hashed = hash.argon2("secret", "random-salt")
println(hashed)