* updates go-jose to new updated repo due to migration - updated from /square/go-jose to /go-jose/go-jose - updates to v2.6.3 - addresses CVE-2016-9123 and CVE-2016-9121 - fixes tests that were adjusting for a 1s delay * revert 299>300 in op_test.go
41 lines
897 B
Go
41 lines
897 B
Go
package crypto
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"hash"
|
|
|
|
"gopkg.in/go-jose/go-jose.v2"
|
|
)
|
|
|
|
var ErrUnsupportedAlgorithm = errors.New("unsupported signing algorithm")
|
|
|
|
func GetHashAlgorithm(sigAlgorithm jose.SignatureAlgorithm) (hash.Hash, error) {
|
|
switch sigAlgorithm {
|
|
case jose.RS256, jose.ES256, jose.PS256:
|
|
return sha256.New(), nil
|
|
case jose.RS384, jose.ES384, jose.PS384:
|
|
return sha512.New384(), nil
|
|
case jose.RS512, jose.ES512, jose.PS512:
|
|
return sha512.New(), nil
|
|
default:
|
|
return nil, fmt.Errorf("%w: %q", ErrUnsupportedAlgorithm, sigAlgorithm)
|
|
}
|
|
}
|
|
|
|
func HashString(hash hash.Hash, s string, firstHalf bool) string {
|
|
if hash == nil {
|
|
return s
|
|
}
|
|
//nolint:errcheck
|
|
hash.Write([]byte(s))
|
|
size := hash.Size()
|
|
if firstHalf {
|
|
size = size / 2
|
|
}
|
|
sum := hash.Sum(nil)[:size]
|
|
return base64.RawURLEncoding.EncodeToString(sum)
|
|
}
|