-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
52 lines (42 loc) · 1.04 KB
/
example_test.go
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
package cloudkms_test
import (
"context"
"crypto/rand"
"crypto/x509"
"fmt"
"log"
"math/big"
kms "cloud.google.com/go/kms/apiv1"
"github.com/atotto/cloudkms"
)
func Example() {
ctx := context.Background()
client, err := kms.NewKeyManagementClient(ctx)
if err != nil {
log.Fatal(err)
}
signer, err := cloudkms.NewSigner(client, "projects/<project>/locations/<location>/keyRings/<keyRing>/cryptoKeys/<key>/cryptoKeyVersions/<version>")
if err != nil {
log.Fatal(err)
}
rootCa := &x509.Certificate{
SerialNumber: big.NewInt(1),
// TODO: fill
}
data, _ := x509.CreateCertificate(rand.Reader, rootCa, rootCa, signer.Public(), signer)
cert, _ := x509.ParseCertificate(data)
// Sign
msg := "hello, world"
h := signer.HashFunc().New()
h.Write([]byte(msg))
digest := h.Sum(nil)
signature, err := signer.Sign(rand.Reader, digest, nil)
if err != nil {
log.Fatal(err)
}
// Verify Signature
if err := cert.CheckSignature(cert.SignatureAlgorithm, []byte(msg), signature); err != nil {
log.Fatal(err)
}
fmt.Println("OK")
}