File: cryptoutil_test.go

package info (click to toggle)
golang-github-micromdm-scep 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 404 kB
  • sloc: makefile: 50; sh: 27
file content (58 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cryptoutil

import (
	"crypto"
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/rsa"
	"math/big"
	"testing"
)

func TestGenerateSubjectKeyID(t *testing.T) {
	ecKey, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
	if err != nil {
		t.Fatal(err)
	}
	for _, test := range []struct {
		testName string
		pub      crypto.PublicKey
	}{
		{"RSA", &rsa.PublicKey{N: big.NewInt(123), E: 65537}},
		{"ECDSA", ecKey.Public()},
	} {
		test := test
		t.Run(test.testName, func(t *testing.T) {
			t.Parallel()
			ski, err := GenerateSubjectKeyID(test.pub)
			if err != nil {
				t.Fatal(err)
			}
			if len(ski) != 20 {
				t.Fatalf("unexpected subject public key identifier length: %d", len(ski))
			}
			ski2, err := GenerateSubjectKeyID(test.pub)
			if err != nil {
				t.Fatal(err)
			}
			if !testSKIEq(ski, ski2) {
				t.Fatal("subject key identifier generation is not deterministic")
			}
		})
	}
}

func testSKIEq(a, b []byte) bool {
	if len(a) != len(b) {
		return false
	}

	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}

	return true
}