go.crypto/openpgp: test that hashes are compiled in before using them.
Previously, if Encrypt decided to use a hash function that wasn't compiled in then it would panic. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/12055043
This commit is contained in:
+23
-7
@@ -224,16 +224,32 @@ func Encrypt(ciphertext io.Writer, to []*Entity, signed *Entity, hints *FileHint
|
||||
}
|
||||
}
|
||||
|
||||
hashFunc := candidateHashes[0]
|
||||
// If the hash specified by config is a candidate, we'll use that.
|
||||
configuredHash := config.Hash()
|
||||
for _, h := range candidateHashes {
|
||||
if h == uint8(configuredHash) {
|
||||
hashFunc = h
|
||||
var hash crypto.Hash
|
||||
for _, hashId := range candidateHashes {
|
||||
if h, ok := s2k.HashIdToHash(hashId); ok && h.Available() {
|
||||
hash = h
|
||||
break
|
||||
}
|
||||
}
|
||||
hash, _ := s2k.HashIdToHash(hashFunc)
|
||||
|
||||
// If the hash specified by config is a candidate, we'll use that.
|
||||
if configuredHash := config.Hash(); configuredHash.Available() {
|
||||
for _, hashId := range candidateHashes {
|
||||
if h, ok := s2k.HashIdToHash(hashId); ok && h == configuredHash {
|
||||
hash = h
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hash == 0 {
|
||||
hashId := candidateHashes[0]
|
||||
name, ok := s2k.HashIdToString(hashId)
|
||||
if !ok {
|
||||
name = "#" + strconv.Itoa(int(hashId))
|
||||
}
|
||||
return nil, errors.InvalidArgumentError("cannot encrypt because no candidate hash functions are compiled in. (Wanted " + name + " in this case.)")
|
||||
}
|
||||
|
||||
symKey := make([]byte, cipher.KeySize())
|
||||
if _, err := io.ReadFull(config.Random(), symKey); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user