package hash
import (
"crypto"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
)
type Algorithm uint16
const (
None Algorithm = 0
MD5 Algorithm = 1
SHA1 Algorithm = 2
SHA224 Algorithm = 3
SHA256 Algorithm = 4
SHA384 Algorithm = 5
SHA512 Algorithm = 6
Ed25519 Algorithm = 8
)
func (a Algorithm ) String () string {
switch a {
case None :
return "none"
case MD5 :
return "md5"
case SHA1 :
return "sha-1"
case SHA224 :
return "sha-224"
case SHA256 :
return "sha-256"
case SHA384 :
return "sha-384"
case SHA512 :
return "sha-512"
case Ed25519 :
return "null"
default :
return "unknown or unsupported hash algorithm"
}
}
func (a Algorithm ) Digest (b []byte ) []byte {
switch a {
case None :
return nil
case MD5 :
hash := md5 .Sum (b )
return hash [:]
case SHA1 :
hash := sha1 .Sum (b )
return hash [:]
case SHA224 :
hash := sha256 .Sum224 (b )
return hash [:]
case SHA256 :
hash := sha256 .Sum256 (b )
return hash [:]
case SHA384 :
hash := sha512 .Sum384 (b )
return hash [:]
case SHA512 :
hash := sha512 .Sum512 (b )
return hash [:]
default :
return nil
}
}
func (a Algorithm ) Insecure () bool {
switch a {
case None , MD5 , SHA1 :
return true
default :
return false
}
}
func (a Algorithm ) CryptoHash () crypto .Hash {
switch a {
case None :
return crypto .Hash (0 )
case MD5 :
return crypto .MD5
case SHA1 :
return crypto .SHA1
case SHA224 :
return crypto .SHA224
case SHA256 :
return crypto .SHA256
case SHA384 :
return crypto .SHA384
case SHA512 :
return crypto .SHA512
case Ed25519 :
return crypto .Hash (0 )
default :
return crypto .Hash (0 )
}
}
func Algorithms () map [Algorithm ]struct {} {
return map [Algorithm ]struct {}{
None : {},
MD5 : {},
SHA1 : {},
SHA224 : {},
SHA256 : {},
SHA384 : {},
SHA512 : {},
Ed25519 : {},
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .