2014-09-16 21:12:52 +00:00
|
|
|
package cryptomap
|
2014-09-12 20:10:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
2016-09-13 14:40:39 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
// Importing all the currently supported hashes
|
2014-09-12 20:10:10 +00:00
|
|
|
_ "crypto/md5"
|
|
|
|
_ "crypto/sha1"
|
|
|
|
_ "crypto/sha256"
|
|
|
|
_ "crypto/sha512"
|
|
|
|
)
|
|
|
|
|
2016-09-13 14:55:19 +00:00
|
|
|
// DefaultCipher is the crypto cipher default used if none is specified or
|
|
|
|
// specified is unknown.
|
|
|
|
var DefaultCipher = "sha1"
|
|
|
|
|
|
|
|
// Ciphers is the known set of mappings for string to crypto.Hash
|
|
|
|
// use an init() to add custom hash ciphers
|
|
|
|
var Ciphers = map[string]crypto.Hash{
|
|
|
|
"md5": crypto.MD5,
|
|
|
|
"sha1": crypto.SHA1,
|
|
|
|
"sha224": crypto.SHA224,
|
|
|
|
"sha256": crypto.SHA256,
|
|
|
|
"sha384": crypto.SHA384,
|
|
|
|
"sha512": crypto.SHA512,
|
2014-09-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 14:40:39 +00:00
|
|
|
// DetermineHash takes a generic string, like "sha1" and returns the
|
|
|
|
// corresponding crypto.Hash
|
2014-09-12 20:10:10 +00:00
|
|
|
func DetermineHash(str string) (h crypto.Hash) {
|
2016-09-13 14:55:19 +00:00
|
|
|
if h, ok := Ciphers[strings.ToLower(str)]; ok {
|
|
|
|
return h
|
2014-09-12 20:10:10 +00:00
|
|
|
}
|
2016-09-13 14:55:19 +00:00
|
|
|
log.Printf("WARNING: unknown cipher %q. using %q", str, DefaultCipher)
|
|
|
|
return Ciphers[DefaultCipher]
|
2014-09-12 20:10:10 +00:00
|
|
|
}
|