cromo The Cromo Guide
Download PDF IT

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).

cromo
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

cromo
struct Totp

The 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 secret
  • i64 digits: of a code: from 6 to 9
  • i64 period: how many seconds a code lasts
  • crypto.Hash hash: of the HMAC: Sha1, Sha256 or Sha512
  • i64 window: How many periods before and after the one of now verify takes: 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

cromo
string codeAt(i64 unix) const

The code at a time, in seconds of Unix.

#Totp.code

cromo
string code() const

The code of now, as the app of the user shows it.

#Totp.stepAt

cromo
i64 stepAt(string code, i64 unix) const

The 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

cromo
bool verify(string code) const

Whether 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

cromo
bool verifyAt(string code, i64 unix) const

Whether code is the code of the time unix, or of the window around it.

#Totp.uri

cromo
string uri(string account, string issuer) const

The 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

cromo
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

cromo
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

cromo
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

cromo
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.