Skip to content

Commit

Permalink
Extract rand func (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Apr 15, 2024
1 parent 7c42e5f commit 51901bf
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
9 changes: 9 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"crypto/subtle"
"encoding/base64"
Expand Down Expand Up @@ -178,6 +179,14 @@ func doHKDF(key, salt, info []byte) io.Reader {
return hkdf.New(sha512.New384, key, salt, info)
}

func readRand(size int) []byte {
b := make([]byte, size)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
panic(err)
}
return b
}

func b64Decode(dst, src []byte) (n int, err error) {
return base64.RawURLEncoding.Decode(dst, src)
}
Expand Down
6 changes: 1 addition & 5 deletions v1loc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package paseto

import (
"crypto/hmac"
"crypto/rand"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -43,10 +42,7 @@ func V1Encrypt(key []byte, payload, footer any, randBytes []byte) (string, error
// step 3.
b := randBytes
if b == nil {
b = make([]byte, v1locNonce)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return "", fmt.Errorf("read from crypto/rand.Reader: %w", err)
}
b = readRand(v1locNonce)
}

// step 4.
Expand Down
7 changes: 1 addition & 6 deletions v2loc.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package paseto

import (
"crypto/rand"
"errors"
"fmt"
"io"

"golang.org/x/crypto/chacha20poly1305"
)
Expand Down Expand Up @@ -42,10 +40,7 @@ func V2Encrypt(key []byte, payload, footer any, randBytes []byte) (string, error
// step 3.
b := randBytes
if b == nil {
b = make([]byte, v2locNonce)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return "", fmt.Errorf("read from crypto/rand.Reader: %w", err)
}
b = readRand(v2locNonce)
}

// step 4.
Expand Down
6 changes: 1 addition & 5 deletions v3loc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package paseto

import (
"crypto/hmac"
"crypto/rand"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -45,10 +44,7 @@ func V3Encrypt(key []byte, payload, footer any, implicit string, randBytes []byt
// step 3.
n := randBytes
if n == nil {
n = make([]byte, v3locNonce)
if _, err := io.ReadFull(rand.Reader, n); err != nil {
return "", fmt.Errorf("read from crypto/rand.Reader: %w", err)
}
n = readRand(v3locNonce)
}

// step 4.
Expand Down
7 changes: 1 addition & 6 deletions v4loc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package paseto

import (
"crypto/hmac"
"crypto/rand"
"errors"
"fmt"
"io"
"strings"
)

Expand Down Expand Up @@ -45,10 +43,7 @@ func V4Encrypt(key []byte, payload, footer any, implicit string, randBytes []byt
// step 3.
n := randBytes
if n == nil {
n = make([]byte, v4locNonce)
if _, err := io.ReadFull(rand.Reader, n); err != nil {
return "", fmt.Errorf("read from crypto/rand.Reader: %w", err)
}
n = readRand(v4locNonce)
}

// step 4.
Expand Down

0 comments on commit 51901bf

Please sign in to comment.