signing/signing

This module implements basic signing operations.

import os, json

block:
  let
    key = SecretKey("secret-key")
    s = initSigner(key, salt = "itsdangerous.Signer")
    sig = s.sign("my string")
  doAssert sig == "my string.wh6tMHxLgJqB6oY1uT73iMlyrOA"
  doAssert s.unsign(sig) == "my string"
  doAssert validate(s, sig)

block:
  let
    key = SecretKey("secret-key")
    s = initTimedSigner(key, salt = "activate",
        digestMethod = Sha1Type)
    sig = s.sign("my string")
  sleep(6000)
  doAssertRaises(SignatureExpiredError):
    discard s.unsign(sig, 5) == "my string"

block:
  let
    key = SecretKey("secret-key")
    s = initSigner(key, salt = "activate",
        digestMethod = Sha1Type)
    sig {.used.} = s.sign( $ %*[1, 2, 3])
  doAssertRaises(BadSignatureError):
    discard s.unsign("[1, 2, 3].sdhfghjkjhdfghjigf")

Types

BaseDigestType = sha1 | sha2 | keccak | ripemd | blake2
  Source Edit
BaseDigestMethodType = enum
  Sha1Type, Sha224Type, Sha384Type, Sha512Type, Sha512_224Type, Sha512_256Type,
  Keccak224Type, Keccak256Type, Keccak384Type, Keccak512Type, Sha3_224Type,
  Sha3_256Type, Sha3_384Type, Sha3_512Type, Ripemd128Type, Ripemd160Type,
  Ripemd256Type, Ripemd320Type, Blake2_224Type, Blake2_256Type, Blake2_384Type,
  Blake2_512Type
  Source Edit
KeyDerivation = enum
  Concat, MoreConcat, KeyHmac, None
  Source Edit
Signer = object
  secretKey: SecretKey
  salt: string
  sep: char
  keyDerivation: KeyDerivation
  digestMethod: BaseDigestMethodType
  Source Edit
TimedSigner = object
  secretKey: SecretKey
  salt: string
  sep: char
  keyDerivation: KeyDerivation
  digestMethod: BaseDigestMethodType
  Source Edit

Procs

proc getSignatureEncode(s: Signer | TimedSigner; value: openArray[byte]): string
  Source Edit
proc getSignatureDecode(s: Signer | TimedSigner): string
  Source Edit
proc sign(s: Signer; value: string): string {...}{.raises: [], tags: [].}
  Source Edit
proc sign(s: TimedSigner; value: string): string {...}{.raises: [],
    tags: [TimeEffect].}
  Source Edit
proc unsign(s: Signer | TimedSigner; signedValue: string): string
  Source Edit
proc unsign(s: TimedSigner; signedValue: string; max_age: Natural): string {...}{.raises: [
    ValueError, Exception, BadTimeSignatureError, SignatureExpiredError],
    tags: [TimeEffect].}
  Source Edit
proc validate(s: Signer; signedValue: string): bool {...}{.raises: [ValueError],
    tags: [].}
  Source Edit

Funcs

func initSigner(secretKey: SecretKey; salt = DefaultSalt; sep = DefaultSep;
                keyDerivation = DefaultKeyDerivation;
                digestMethod = DefaultDigestMethodType): Signer {...}{.
    raises: [ValueError], tags: [].}
  Source Edit
func initTimedSigner(secretKey: SecretKey; salt = DefaultSalt; sep = DefaultSep;
                     keyDerivation = DefaultKeyDerivation;
                     digestMethod = DefaultDigestMethodType): TimedSigner {...}{.
    raises: [ValueError], tags: [].}
  Source Edit