Skip to content

Commit

Permalink
Merge pull request #16 from samdfonseca/sam-injectable-now
Browse files Browse the repository at this point in the history
adds EncryptAndSignAtTime func to inject signed at time
  • Loading branch information
kennyp authored Jan 19, 2024
2 parents 498e490 + cff816a commit 303da6a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions fernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,28 @@ func genhmac(q, p, k []byte) {
h.Sum(q)
}

// EncryptAndSign encrypts and signs msg with key k and returns the resulting
// fernet token. If msg contains text, the text should be encoded
// with UTF-8 to follow fernet convention.
func EncryptAndSign(msg []byte, k *Key) (tok []byte, err error) {
// EncryptAndSignAtTime encrypts and signs msg with key k at timestamp signedAt
// and returns the resulting fernet token. If msg contains text, the text
// should be encoded with UTF-8 to follow fernet convention.
func EncryptAndSignAtTime(msg []byte, k *Key, signedAt time.Time) (tok []byte, err error) {
iv := make([]byte, aes.BlockSize)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
b := make([]byte, encodedLen(len(msg)))
n := gen(b, msg, iv, time.Now(), k)
n := gen(b, msg, iv, signedAt, k)
tok = make([]byte, encoding.EncodedLen(n))
encoding.Encode(tok, b[:n])
return tok, nil
}

// EncryptAndSign encrypts and signs msg with key k and returns the resulting
// fernet token. If msg contains text, the text should be encoded
// with UTF-8 to follow fernet convention.
func EncryptAndSign(msg []byte, k *Key) (tok []byte, err error) {
return EncryptAndSignAtTime(msg, k, time.Now())
}

// VerifyAndDecrypt verifies that tok is a valid fernet token that was signed
// with a key in k at most ttl time ago only if ttl is greater than zero.
// Returns the message contained in tok if tok is valid, otherwise nil.
Expand Down

0 comments on commit 303da6a

Please sign in to comment.