1
2
3
4
5 package rsa
6
7 import (
8 "bytes"
9 "crypto"
10 "crypto/x509/pkix"
11 "encoding/asn1"
12 "testing"
13 )
14
15 func TestHashPrefixes(t *testing.T) {
16 prefixes := map[crypto.Hash]asn1.ObjectIdentifier{
17
18
19
20
21
22
23
24 crypto.MD5: {1, 2, 840, 113549, 2, 5},
25 crypto.SHA1: {1, 3, 14, 3, 2, 26},
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 crypto.SHA224: {2, 16, 840, 1, 101, 3, 4, 2, 4},
45 crypto.SHA256: {2, 16, 840, 1, 101, 3, 4, 2, 1},
46 crypto.SHA384: {2, 16, 840, 1, 101, 3, 4, 2, 2},
47 crypto.SHA512: {2, 16, 840, 1, 101, 3, 4, 2, 3},
48 crypto.SHA512_224: {2, 16, 840, 1, 101, 3, 4, 2, 5},
49 crypto.SHA512_256: {2, 16, 840, 1, 101, 3, 4, 2, 6},
50 crypto.SHA3_224: {2, 16, 840, 1, 101, 3, 4, 2, 7},
51 crypto.SHA3_256: {2, 16, 840, 1, 101, 3, 4, 2, 8},
52 crypto.SHA3_384: {2, 16, 840, 1, 101, 3, 4, 2, 9},
53 crypto.SHA3_512: {2, 16, 840, 1, 101, 3, 4, 2, 10},
54 }
55
56 for h, oid := range prefixes {
57 want, err := asn1.Marshal(struct {
58 HashAlgorithm pkix.AlgorithmIdentifier
59 Hash []byte
60 }{
61 HashAlgorithm: pkix.AlgorithmIdentifier{
62 Algorithm: oid,
63 Parameters: asn1.NullRawValue,
64 },
65 Hash: make([]byte, h.Size()),
66 })
67 if err != nil {
68 t.Fatal(err)
69 }
70 want = want[:len(want)-h.Size()]
71 got := hashPrefixes[h.String()]
72 if !bytes.Equal(got, want) {
73 t.Errorf("%s: got %x, want %x", h, got, want)
74 }
75 }
76 }
77
View as plain text