crypto
SHA and MD5 digests, HMAC signatures, secure random numbers and password hashing.
import crypto
This package gives the basic cryptography tools, all of them written in Salam. For the digest of a piece of text the ...Hex functions are the simplest way; the ...Bytes functions write the digest into memory you pass in yourself.
Never store a user password raw or under a plain digest; use HashPassword and VerifyPassword. To compare tokens, use SecretEquals rather than ==. To encrypt data, see the tls package.
Types
Sha256
struct Sha256| Name | Type | Description |
|---|---|---|
h | u32[] | The internal state. |
blk | u8[] | The partial block. |
blen | int = 0 | The length of the partial block. |
total | u64 = 0 | The total number of bytes. |
import crypto
import encoding
import mem
func main:
println crypto.Sha256Hex("abc")
println crypto.Sha512Hex("abc").len()
println crypto.Sha1Hex("abc")
println crypto.Sha1Base64("abc")
println crypto.Md5Hex("abc")
text := "abc"
out := mem.Allocate(64 as u64)
crypto.Sha256Bytes(text as void*, 3, out)
println encoding.Base64EncodeBytes(out, 32)
crypto.Sha384Bytes(text as void*, 3, out)
println encoding.Base64EncodeBytes(out, 48)
crypto.Sha512Bytes(text as void*, 3, out)
println encoding.Base64EncodeBytes(out, 16).len()
crypto.Sha1Bytes(text as void*, 3, out)
println encoding.Base64EncodeBytes(out, 20)
crypto.Md5Bytes(text as void*, 3, out)
println encoding.Base64EncodeBytes(out, 16)
mut h := crypto.Sha256New()
crypto.Sha256Update(h, "a" as void*, 1)
crypto.Sha256Update(h, "bc" as void*, 2)
crypto.Sha256Final(h, out)
println encoding.Base64EncodeBytes(out, 32)
mut b := crypto.Sha512New()
crypto.Sha512Update(b, text as void*, 3)
crypto.Sha512Final(b, out)
println b.outlen
mut s := crypto.Sha384New()
crypto.Sha512Update(s, text as void*, 3)
crypto.Sha512Final(s, out)
println s.outlen, encoding.Base64EncodeBytes(out, 48)
mem.Free(out)
println crypto.HashStr("hello") == crypto.HashStr("hello"), crypto.HashStr("a") != crypto.HashStr("b")
println crypto.HashBytes(text as void*, 3 as u64) == crypto.HashStr("abc")
println crypto.HashInt(42 as u64) != crypto.HashInt(43 as u64)
endSha512
struct Sha512| Name | Type | Description |
|---|---|---|
h | u64[] | The internal state. |
blk | u8[] | The partial block. |
blen | int = 0 | The length of the partial block. |
total | u64 = 0 | The total number of bytes. |
outlen | int = 64 | The digest length: 64 for SHA-512 and 48 for SHA-384. |
The example for this item is shown with the ones above: crypto-Hash.salam
Functions
Sha256Hex
func Sha256Hex(data: str): strThe example for this item is shown with the ones above: crypto-Hash.salam
Pbkdf2HmacSha256Hex
func Pbkdf2HmacSha256Hex(password: str, salt: str, iterations: int, keylen: int): strimport crypto
import str
import mem
func main:
stored := crypto.HashPassword("s3cret-password")
println str.StartsWith(stored, "pbkdf2-sha256$")
println crypto.VerifyPassword("s3cret-password", stored), crypto.VerifyPassword("wrong", stored)
println stored != crypto.HashPassword("s3cret-password")
println crypto.Pbkdf2HmacSha256Hex("password", "salt", 1, 32)
println crypto.SecretEquals("abc", "abc"), crypto.SecretEquals("abc", "xyz")
println crypto.RandomHex(16).len(), crypto.RandomToken(32).len() > 0
b := mem.Allocate(8 as u64)
crypto.RandomBytes(b, 8)
mem.Free(b)
endHashPassword
func HashPassword(password: str): strpbkdf2-sha256$iterations$salt$hash to keep in your database. The output is different every time.The example for this item is shown with the ones above: crypto-Password.salam
VerifyPassword
func VerifyPassword(password: str, encoded: str): boolHashPassword made.The example for this item is shown with the ones above: crypto-Password.salam
SecretEquals
func SecretEquals(a: str, b: str): boolThe example for this item is shown with the ones above: crypto-Password.salam
HmacSha256Hex
func HmacSha256Hex(key: str, msg: str): strimport crypto
import encoding
import mem
func main:
key := "key"
message := "The quick brown fox jumps over the lazy dog"
println crypto.HmacSha256Hex(key, message)
println crypto.HmacSha1Hex(key, message)
println crypto.HmacSha1Base64(key, message)
println crypto.HmacSha384Hex(key, message).len(), crypto.HmacSha512Hex(key, message).len()
out := mem.Allocate(64 as u64)
crypto.HmacSha256Bytes(key, message, out)
a := encoding.Base64EncodeBytes(out, 32)
crypto.HmacSha256Raw(key as void*, 3, message as void*, message.len(), out)
println a == encoding.Base64EncodeBytes(out, 32)
crypto.HmacSha1Bytes(key, message, out)
b := encoding.Base64EncodeBytes(out, 20)
crypto.HmacSha1Raw(key as void*, 3, message as void*, message.len(), out)
println b == encoding.Base64EncodeBytes(out, 20), b == crypto.HmacSha1Base64(key, message)
crypto.HmacSha384Bytes(key, message, out)
c := encoding.Base64EncodeBytes(out, 48)
crypto.HmacSha384Raw(key as void*, 3, message as void*, message.len(), out)
println c == encoding.Base64EncodeBytes(out, 48)
crypto.HmacSha512Bytes(key, message, out)
d := encoding.Base64EncodeBytes(out, 64)
crypto.HmacSha512Raw(key as void*, 3, message as void*, message.len(), out)
println d == encoding.Base64EncodeBytes(out, 64)
mem.Free(out)
endSha1Hex
func Sha1Hex(data: str): strThe example for this item is shown with the ones above: crypto-Hash.salam
Sha1Base64
func Sha1Base64(data: str): strThe example for this item is shown with the ones above: crypto-Hash.salam
Sha1Bytes
func Sha1Bytes(p: void*, n: int, out: void*)The example for this item is shown with the ones above: crypto-Hash.salam
Sha256Bytes
func Sha256Bytes(p: void*, n: int, out: void*)The example for this item is shown with the ones above: crypto-Hash.salam
Sha384Bytes
func Sha384Bytes(p: void*, n: int, out: void*)The example for this item is shown with the ones above: crypto-Hash.salam
Sha512Bytes
func Sha512Bytes(p: void*, n: int, out: void*)The example for this item is shown with the ones above: crypto-Hash.salam
Sha256New
func Sha256New(): Sha256The example for this item is shown with the ones above: crypto-Hash.salam
Sha256Update
func Sha256Update(s &: Sha256, p: void*, n: int)The example for this item is shown with the ones above: crypto-Hash.salam
Sha256Final
func Sha256Final(s: Sha256, out: void*)The example for this item is shown with the ones above: crypto-Hash.salam
Sha384New
func Sha384New(): Sha512The example for this item is shown with the ones above: crypto-Hash.salam
Sha512New
func Sha512New(): Sha512The example for this item is shown with the ones above: crypto-Hash.salam
Sha512Update
func Sha512Update(s &: Sha512, p: void*, n: int)The example for this item is shown with the ones above: crypto-Hash.salam
Sha512Final
func Sha512Final(s: Sha512, out: void*)outlen.The example for this item is shown with the ones above: crypto-Hash.salam
HashBytes
func HashBytes(p: void*, n: u64): u64The example for this item is shown with the ones above: crypto-Hash.salam
HashStr
func HashStr(s: str): u64The example for this item is shown with the ones above: crypto-Hash.salam
HashInt
func HashInt(x: u64): u64The example for this item is shown with the ones above: crypto-Hash.salam
RandomHex
func RandomHex(n: int): strThe example for this item is shown with the ones above: crypto-Password.salam
RandomToken
func RandomToken(n: int): strThe example for this item is shown with the ones above: crypto-Password.salam
RandomBytes
func RandomBytes(p: void*, n: int)The example for this item is shown with the ones above: crypto-Password.salam
Md5Bytes
func Md5Bytes(p: void*, n: int, out: void*)The example for this item is shown with the ones above: crypto-Hash.salam
Md5Hex
func Md5Hex(data: str): strThe example for this item is shown with the ones above: crypto-Hash.salam
HmacSha256Raw
func HmacSha256Raw(key: void*, klen: int, msg: void*, mlen: int, out: void*)The example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha384Raw
func HmacSha384Raw(key: void*, klen: int, msg: void*, mlen: int, out: void*)The example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha512Raw
func HmacSha512Raw(key: void*, klen: int, msg: void*, mlen: int, out: void*)The example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha256Bytes
func HmacSha256Bytes(key: str, msg: str, out: void*)The example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha384Bytes
func HmacSha384Bytes(key: str, msg: str, out: void*)The example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha512Bytes
func HmacSha512Bytes(key: str, msg: str, out: void*)The example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha384Hex
func HmacSha384Hex(key: str, msg: str): strThe example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha512Hex
func HmacSha512Hex(key: str, msg: str): strThe example for this item is shown with the ones above: crypto-Hmac.salam
Sha512Hex
func Sha512Hex(data: str): strThe example for this item is shown with the ones above: crypto-Hash.salam
HmacSha1Raw
func HmacSha1Raw(key: void*, klen: int, msg: void*, mlen: int, out: void*)The example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha1Bytes
func HmacSha1Bytes(key: str, msg: str, out: void*)The example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha1Hex
func HmacSha1Hex(key: str, msg: str): strThe example for this item is shown with the ones above: crypto-Hmac.salam
HmacSha1Base64
func HmacSha1Base64(key: str, msg: str): strThe example for this item is shown with the ones above: crypto-Hmac.salam