-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add hash option to token strategy
- Loading branch information
Showing
6 changed files
with
117 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package internal | ||
|
||
import ( | ||
"crypto" | ||
"crypto/hmac" | ||
"encoding/base64" | ||
"hash" | ||
"sync" | ||
) | ||
|
||
// Hasher represents a hash generator. | ||
type Hasher interface { | ||
Hash(string) string | ||
} | ||
|
||
// PlainTextHasher implements the hasher interface and return input as is without hashing it. | ||
type PlainTextHasher struct{} | ||
|
||
// Hash return str as is without hashing it. | ||
func (p PlainTextHasher) Hash(str string) string { return str } | ||
|
||
// HMACHasher implements the hasher interface and hash input using HMAC hashing alg. | ||
type HMACHasher struct { | ||
p *sync.Pool | ||
} | ||
|
||
// Hash str and return output as base64. | ||
func (hm HMACHasher) Hash(str string) string { | ||
h := hm.p.Get().(hash.Hash) | ||
if _, err := h.Write([]byte(str)); err != nil { | ||
// Write() on hash never fails | ||
panic(err) | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(h.Sum(nil)) | ||
} | ||
|
||
// NewHMACHasher return new hmac hasher instance. | ||
func NewHMACHasher(h crypto.Hash, key []byte) *HMACHasher { | ||
return &HMACHasher{ | ||
p: &sync.Pool{ | ||
New: func() interface{} { | ||
return hmac.New(h.New, key) | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters