src/prologue/signing/signing

Source   Edit  

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

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  
BaseDigestType = sha1 | sha2 | keccak | ripemd | blake2
Source   Edit  
KeyDerivation = enum
  Concat, MoreConcat, KeyHmac, None
Source   Edit  
Signer = object
Source   Edit  
TimedSigner = object
Source   Edit  

Consts

DefaultDigestMethodType = Sha1Type
Source   Edit  
DefaultKeyDerivation = MoreConcat
Source   Edit  
DefaultSep = '.'
Source   Edit  

Procs

proc getSignatureDecode(s: Signer | TimedSigner): string
Source   Edit  
proc getSignatureEncode(s: Signer | TimedSigner; value: openArray[byte]): string
Source   Edit  
func initSigner(secretKey: SecretKey; salt = DefaultSalt; sep = DefaultSep;
                keyDerivation = DefaultKeyDerivation;
                digestMethod = DefaultDigestMethodType): Signer {.
    ...raises: [ValueError], tags: [], forbids: [].}
Source   Edit  
func initTimedSigner(secretKey: SecretKey; salt = DefaultSalt; sep = DefaultSep;
                     keyDerivation = DefaultKeyDerivation;
                     digestMethod = DefaultDigestMethodType): TimedSigner {.
    ...raises: [ValueError], tags: [], forbids: [].}
Source   Edit  
proc sign(s: Signer; value: string): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc sign(s: TimedSigner; value: string): string {....raises: [],
    tags: [TimeEffect], forbids: [].}
Source   Edit  
proc unsign(s: Signer | TimedSigner; signedValue: string): string
Source   Edit  
proc unsign(s: TimedSigner; signedValue: string; max_age: Natural): string {....raises: [
    Exception, BadTimeSignatureError, ValueError, SignatureExpiredError],
    tags: [TimeEffect], forbids: [].}
Source   Edit  
proc validate(s: Signer; signedValue: string): bool {....raises: [], tags: [],
    forbids: [].}
Source   Edit