Turtle

Library › crypt — hashes, encodings, passwords and encryption

crypt — hashes, encodings, passwords and encryption

import crypt

Hashes, encodings, passwords and encryption: fingerprints of text or files, Base64 and hex, safe password storage, and boxes locked with a passphrase.

Functions

hash, filehash, hmac, encode, decode, uuid, token, passwordhash, passwordcheck, encrypt, decrypt.

hash[text, algorithm]

A fingerprint of the text: the same text always gives the same hash. algorithm optional: "sha256" (the default), "sha512", "sha1" or "md5" (sha1 and md5 only to match old systems) Gives back: hex text.

Example

import crypt
h = hash["hello"]
h = "hello" hash "sha512"
show h .

Output

9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043

filehash[path, algorithm]

hash of a file's contents (a checksum), read a piece at a time.

Example

[write] release.zip
"release"
[end]
sum = filehash["release.zip"]
show length of sum .

Output

64

hmac[text, key, algorithm]

A signature: the hash of text with a secret key, as web APIs and webhooks use. algorithm as for hash.

Example

body = "hello"
secret = "key"
sig = body hmac secret
show length of sig .

Output

64

encode[text, how]

how: "base64" (the default), "base64url" (for links, no padding) or "hex".

Example

import crypt
b = encode["hi"]
show b .

Output

aGk=

decode[text, how]

The other way; how as for encode.

Example

import crypt
s = decode["aGk="]
show s .

Output

hi

uuid[]

A random id (a version 4 UUID): 36 characters, different every time.

Example

u = uuid[]
show length of u .

Output

36

token[length]

Random letters and digits from the system's secure source, for keys, session ids and reset links. length: 32 if left out (1 to 4096).

Example

import crypt
key = token[]
show key .

Output

NWv4EyT4KonoemTX8AcG1SKv7COLCbZT

The token is random, so it differs on every run.

passwordhash[password]

What to store instead of a password: salted PBKDF2-SHA256 (600,000 rounds), different each time for the same password.

Example

pw = "secret"
stored = passwordhash[pw]
typed = "secret"
if ] passwordcheck[typed, stored] [
    show "password accepted" .
if [end]

Output

password accepted

passwordcheck[password, stored]

Whether password is the one stored was made from.

Example

pw = "secret"
stored = passwordhash[pw]
show passwordcheck["wrong", stored] .
show passwordcheck["secret", stored] .

Output

false
true

encrypt[text, passphrase]

Locks text with a passphrase (AES-256-GCM, the key made from the passphrase with PBKDF2).

Example

notes = "my note"
phrase = "pass"
box = encrypt[notes, phrase]
notes = decrypt[box, phrase]
show notes .

Output

my note

decrypt[box, passphrase]

Opens what encrypt made.

Example

notes = "my note"
phrase = "pass"
box = encrypt[notes, phrase]
notes = decrypt[box, phrase]
show notes .

Output

my note
Top