Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
export Crypto.HmacSum
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Mar 1, 2017
1 parent 39e151e commit a862739
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 2 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/teambition/gear-auth/jwt"
)

const Version = "1.4.4"

// TokenExtractor is a function that takes a gear.Context as input and
// returns either a string token or an empty string. Default to:
//
Expand Down
13 changes: 7 additions & 6 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func New(salt []byte) *Crypto {

// AESKey returns a string key to encrypt or decrypt text.
func (c *Crypto) AESKey(a, b string) (key string) {
buf := c.hmacSum(append(c.hmacSum([]byte(a)), []byte(b)...))
buf := c.HmacSum(append(c.HmacSum([]byte(a)), []byte(b)...))
return base64.RawURLEncoding.EncodeToString(buf)
}

// SignPass returns a string checkPass by the user' name and pass.
func (c *Crypto) SignPass(name, pass string) (checkPass string) {
iv := RandBytes(8)
b := c.signPass(iv, append(c.hmacSum([]byte(name)), []byte(pass)...))
b := c.signPass(iv, append(c.HmacSum([]byte(name)), []byte(pass)...))
return base64.RawURLEncoding.EncodeToString(b)
}

Expand All @@ -52,13 +52,13 @@ func (c *Crypto) VerifyPass(name, pass, checkPass string) bool {
if err != nil {
return false
}
b := c.signPass(a[32:], append(c.hmacSum([]byte(name)), []byte(pass)...))
b := c.signPass(a[32:], append(c.HmacSum([]byte(name)), []byte(pass)...))
return subtle.ConstantTimeCompare(a, b) == 1
}

// Encrypt encrypt data with key
func (c *Crypto) Encrypt(key, data []byte) ([]byte, error) {
k := c.hmacSum(key)
k := c.HmacSum(key)
size := aes.BlockSize
block, err := aes.NewCipher(k)
if err != nil {
Expand All @@ -85,7 +85,7 @@ func (c *Crypto) Decrypt(key, cipherData []byte) ([]byte, error) {
return nil, errors.New("invalid data")
}

k := c.hmacSum(key)
k := c.HmacSum(key)
checkSum := cipherData[len(cipherData)-sha1.Size:]
cipherData = cipherData[:len(cipherData)-sha1.Size]
block, err := aes.NewCipher(k)
Expand Down Expand Up @@ -128,7 +128,8 @@ func (c *Crypto) DecryptText(key, cipherText string) (string, error) {
return string(data), nil
}

func (c *Crypto) hmacSum(data []byte) []byte {
// HmacSum return hash bytes with salt.
func (c *Crypto) HmacSum(data []byte) []byte {
h := hmac.New(sha256.New, c.salt)
h.Write(data)
return h.Sum(nil)
Expand Down

0 comments on commit a862739

Please sign in to comment.