One-time passwords
import "std/data" (included in import "std") · namespace std.data
Two-factor codes: Totp (RFC 6238, time-based: what Google Authenticator, 1Password and Authy show) and Hotp (RFC 4226, counter-based). Secrets travel as Base32. Needs OpenSSL's libcrypto, as Crypto does.
grav
import "std"
fn main() {
let secret = Base32.encodeString("12345678901234567890") // normally Totp.generateSecret()
let totp = new Totp(secret)
let at: long = 1111111109
let code = totp.codeAt(at)
print(code)
print(totp.verifyAt(code, at)) // the step it matched: store it
print(totp.verifyAt(code, at, totp.stepAt(at))) // the same code again: refused
print(totp.uri("Acme", "ada@acme.test"))
print(Hotp.code(Bytes.fromString("12345678901234567890"), 1))
}081804
37037036
-1
otpauth://totp/Acme:ada%40acme.test?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=Acme&algorithm=SHA1&digits=6&period=30
287082- A secret is
Totp.generateSecret(): 20 random bytes (160 bits) in Base32. Show it once (or as theuriin a QR code) and store it encrypted. verify(code, lastStep, window)accepts a code fromwindowsteps either side of now (1 by default: a phone clock a little off still works) and only afterlastStep. Store the step it returns, and each code works once. It gives -1 for a wrong code. Codes are compared in constant time.- Settings per
Totp:digits(6 to 10),period(30 seconds), andalgorithm("sha1", which the apps assume;"sha256","sha512"). Hotp.verify(key, code, counter, lookAhead)returns the counter that matched; the server then stores the one after it.
API
class Hotp
Counter-based one-time passwords (RFC 4226).
Static members
static fn code(key: Bytes, counter: long, digits: int = 6, algorithm: string = "sha1") -> string— The code forcounterunderkey("sha1", "sha256" or "sha512"; 6 to 10 digits).static fn verify(key: Bytes, code: string, counter: long, lookAhead: int = 0, digits: int = 6, algorithm: string = "sha1") -> long— The counter in [counter, counter + lookAhead] whose code iscode, or -1 (the server then stores the counter after it).
class Totp
Time-based one-time passwords (RFC 6238) for one secret.
Constructors
new Totp(secret: string, digits: int = 6, period: long = 30, algorithm: string = "sha1")—secretin Base32, as authenticator apps take it.
Static members
static fn generateSecret(bytes: int = 20) -> string— A new random secret (byteslong: 20 is 160 bits, as RFC 4226 advises), in Base32.
Fields
key: Bytesdigits: intperiod: long— Seconds per step.algorithm: string— "sha1" (what the apps assume), "sha256" or "sha512".
Methods
fn stepAt(unixSeconds: long) -> long— The step a unix time falls in.fn codeAtStep(step: long) -> string— The code for a step.fn codeAt(unixSeconds: long) -> string— The code at a unix time.fn now() -> string— The code right now.fn secondsLeft() -> long— Seconds until the current code changes.fn verify(code: string, lastStep: long = -1, window: int = 1) -> long— The stepcodebelongs to, withinwindowsteps either side of now (a clock a little off still works) and afterlastStep(store it: a code is then good once); -1 when it fits none.fn verifyAt(code: string, unixSeconds: long, lastStep: long = -1, window: int = 1) -> long— verify at a given unix time.fn uri(issuer: string, account: string) -> string— The otpauth:// address an authenticator app reads from a QR code.
class Otp
Static members
static fn clean(code: string, digits: int) -> string—codewithout spaces, or "" when it is notdigitsdigits.static fn escape(s: string) -> string— Percent-encoding for an otpauth label or parameter.