Capy-Bearer Documentation

crypto_equal

lib call

Capy

function crypto_equal(left : string, right : string) bool

Description

crypto_equal() compares byte strings in constant time. Strings of different lengths are never equal, also when the longer one only adds zero bytes. The time depends on the length of the longer string, not on where the strings differ.

Use it to compare secrets such as HMACs and tokens. Do not use == to compare secrets.

Parameters

left : the expected byte string

right : the submitted byte string

Return Values

true if both byte strings are equal.

Example

Capy

var expected := hmac_sha256("key", "payload")
if crypto_equal(expected, hmac_sha256("key", "payload")) { print("valid\n") } else { print("invalid\n") }

Output

valid