std.otp
One-time codes, the six digits of the authenticator apps: Google Authenticator, Microsoft Authenticator, Authy, 1Password and the others. A service gives a secret to the app of a user once, and then checks the code that the app shows, which changes every 30 seconds (TOTP, RFC 6238), or at each use (HOTP, RFC 4226).
import std.otp
// Once, when the user turns the second factor on:
string secret = otp.newSecret() // kept with the user, as a password
string link = otp.totp(secret).uri("anna@example.com", "Example")
// ... shown as a QR code, which the app reads
// At each login, after the password:
if (otp.totp(secret).verify(code)) {
// the user has the phone
}The guide: Networking and HTTP.
Made from the documentation in the sources of the library.
#Types
#Totp
struct TotpThe codes that change with the time (TOTP): what the authenticator apps show. It is made by totp from the secret of a user, with what the apps expect: 6 digits, a new code every 30 seconds, SHA-1. The other fields are for the services that were set otherwise; an app must be given the same ones, with uri.
list<u8> secret: the bytes of the secreti64 digits: of a code: from 6 to 9i64 period: how many seconds a code lastscrypto.Hash hash: of the HMAC: Sha1, Sha256 or Sha512i64 window: How many periods before and after the one of nowverifytakes: 1 at the start, so that a code typed a little late, or a phone whose clock is a few seconds off, still passes. 0 takes the code of now alone.
#Totp.codeAt
string codeAt(i64 unix) constThe code at a time, in seconds of Unix.
#Totp.code
string code() constThe code of now, as the app of the user shows it.
#Totp.stepAt
i64 stepAt(string code, i64 unix) constThe period whose code is code, among those of the window around the time unix, or -1 when it is the code of none. The periods are counted from 1970: a program that keeps the last one used by each user, and takes only a later one, makes each code good once, so that who sees a code typed cannot use it again in the same half minute.
#Totp.verify
bool verify(string code) constWhether code is the code of now, or of a period of the window around it. The spaces of a code typed as "123 456" are passed over. A service counts the wrong codes of a user and stops after a few: six digits are a million tries.
#Totp.verifyAt
bool verifyAt(string code, i64 unix) constWhether code is the code of the time unix, or of the window around it.
#Totp.uri
string uri(string account, string issuer) constThe address that gives the secret to an app, "otpauth://totp/...": shown as a QR code, the app reads it and from then on shows the codes. account is the name of the user in the app, as the address of email, and issuer the name of the service. It holds the secret: it is shown once, to the user, and not kept.
#Functions
#newSecret
string newSecret()A new secret for a user: 20 random bytes, as text in base32, the form that the apps take. It is kept on the server with the user, and must stay secret as a password does: who has it makes the codes.
#totp
Totp totp(string secret)The codes of a secret, as the authenticator apps make them: secret is the text in base32 that newSecret gave, in upper or lower case, with or without spaces. Throws an Error for a text that is not base32.
#hotp
string hotp(list<u8> secret, i64 counter, i64 digits)The code of a counter (HOTP, RFC 4226), for the tokens that give a new code at each press: secret is the bytes of the secret, digits from 6 to 9. The server keeps the counter of each token, and moves it on after a code that passes.
#hotpCounter
i64 hotpCounter(list<u8> secret, string code, i64 counter, i64 ahead)The counter whose code is code, among counter and the ahead after it, or -1: a token pressed without logging in is ahead of the server. The server then keeps the one found plus 1.