///|
/// Raised when bytes are not a secret. It never has the bytes.
#derive(Eq, Debug)
pub(all) suberror SecretError {
  /// The number of bytes is less than 32.
  TooShort(Int32)
}

///|
/// A random text that gives access: the secret of the server, the token of a
/// link, or the cookie value or the view key of a session.
///
/// A secret has no `Show` and no JSON form, and its debug text is always
/// ``, so a log or a failed test does not show it. Only `to_text` gives
/// the text.
struct Secret {
  /// The base64url form of the bytes, without padding.
  text : String
}

///|
/// Makes a secret from 32 random bytes or more. The caller gives the bytes from
/// a random source for cryptography.
pub fn Secret::of_bytes(bytes : Bytes) -> Secret raise SecretError {
  guard bytes.length() >= 32 else { raise TooShort(bytes.length()) }
  { text: @base64.encode(bytes, padding=false, url_safe=true), }
}

///|
/// Reads the text that `to_text` gives, or gives `None` for each other text.
/// There is no error with the text, because the text can be almost a secret.
fn Secret::of_text(text : String) -> Secret? {
  let secret = Secret::of_bytes(@base64.decode(text, url_safe=true)) catch {
    _ => return None
  }
  // The decoder also accepts padding, which `to_text` never gives.
  guard secret.text == text else { return None }
  Some(secret)
}

///|
/// The base64url form of the bytes, without padding. It is safe in an address,
/// a header and a cookie.
pub fn Secret::to_text(self : Secret) -> String {
  self.text
}

///|
/// True when the text is the text of this secret.
///
/// For a text of the same length, the time does not depend on the place of the
/// first difference, so a caller cannot find the secret one character at a
/// time. A text of another length is refused at once: the length is not secret.
pub fn Secret::matches(self : Secret, text : String) -> Bool {
  guard self.text.length() == text.length() else { return false }
  let mut difference = 0
  for i in 0..")
}

///|
pub extend SecretError with Eq::{not_equal, equal}

///|
pub extend SecretError with @debug.Debug::{to_repr}

///|
pub extend Secret with Eq::{not_equal, equal}

///|
pub extend Secret with @debug.Debug::{to_repr}