summaryrefslogtreecommitdiff
path: root/src/pkg/crypto/openpgp/s2k/s2k.go
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-02-18 09:50:58 +0100
committerOndřej Surý <ondrej@sury.org>2011-02-18 09:50:58 +0100
commitc072558b90f1bbedc2022b0f30c8b1ac4712538e (patch)
tree67767591619e4bd8111fb05fac185cde94fb7378 /src/pkg/crypto/openpgp/s2k/s2k.go
parent5859517b767c99749a45651c15d4bae5520ebae8 (diff)
downloadgolang-upstream/2011.02.15.tar.gz
Imported Upstream version 2011.02.15upstream/2011.02.15
Diffstat (limited to 'src/pkg/crypto/openpgp/s2k/s2k.go')
-rw-r--r--src/pkg/crypto/openpgp/s2k/s2k.go66
1 files changed, 40 insertions, 26 deletions
diff --git a/src/pkg/crypto/openpgp/s2k/s2k.go b/src/pkg/crypto/openpgp/s2k/s2k.go
index f369d7ed4..873b33dc0 100644
--- a/src/pkg/crypto/openpgp/s2k/s2k.go
+++ b/src/pkg/crypto/openpgp/s2k/s2k.go
@@ -7,15 +7,12 @@
package s2k
import (
- "crypto/md5"
+ "crypto"
"crypto/openpgp/error"
- "crypto/ripemd160"
- "crypto/sha1"
- "crypto/sha256"
- "crypto/sha512"
"hash"
"io"
"os"
+ "strconv"
)
// Simple writes to out the result of computing the Simple S2K function (RFC
@@ -87,9 +84,13 @@ func Parse(r io.Reader) (f func(out, in []byte), err os.Error) {
return
}
- h := hashFuncFromType(buf[1])
+ hash, ok := HashIdToHash(buf[1])
+ if !ok {
+ return nil, error.UnsupportedError("hash for S2K function: " + strconv.Itoa(int(buf[1])))
+ }
+ h := hash.New()
if h == nil {
- return nil, error.UnsupportedError("hash for S2K function")
+ return nil, error.UnsupportedError("hash not availible: " + strconv.Itoa(int(hash)))
}
switch buf[0] {
@@ -122,25 +123,38 @@ func Parse(r io.Reader) (f func(out, in []byte), err os.Error) {
return nil, error.UnsupportedError("S2K function")
}
-// hashFuncFromType returns a hash.Hash which corresponds to the given hash
-// type byte. See RFC 4880, section 9.4.
-func hashFuncFromType(hashType byte) hash.Hash {
- switch hashType {
- case 1:
- return md5.New()
- case 2:
- return sha1.New()
- case 3:
- return ripemd160.New()
- case 8:
- return sha256.New()
- case 9:
- return sha512.New384()
- case 10:
- return sha512.New()
- case 11:
- return sha256.New224()
+// hashToHashIdMapping contains pairs relating OpenPGP's hash identifier with
+// Go's crypto.Hash type. See RFC 4880, section 9.4.
+var hashToHashIdMapping = []struct {
+ id byte
+ hash crypto.Hash
+}{
+ {1, crypto.MD5},
+ {2, crypto.SHA1},
+ {3, crypto.RIPEMD160},
+ {8, crypto.SHA256},
+ {9, crypto.SHA384},
+ {10, crypto.SHA512},
+ {11, crypto.SHA224},
+}
+
+// HashIdToHash returns a crypto.Hash which corresponds to the given OpenPGP
+// hash id.
+func HashIdToHash(id byte) (h crypto.Hash, ok bool) {
+ for _, m := range hashToHashIdMapping {
+ if m.id == id {
+ return m.hash, true
+ }
}
+ return 0, false
+}
- return nil
+// HashIdToHash returns an OpenPGP hash id which corresponds the given Hash.
+func HashToHashId(h crypto.Hash) (id byte, ok bool) {
+ for _, m := range hashToHashIdMapping {
+ if m.hash == h {
+ return m.id, true
+ }
+ }
+ return 0, false
}