/
ecdsakey.go
82 lines (64 loc) · 1.52 KB
/
ecdsakey.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package csp
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"crypto/x509"
"errors"
"fmt"
)
type ecdsaPrivateKey struct {
privKey *ecdsa.PrivateKey
}
func (k *ecdsaPrivateKey) Bytes() (raw []byte, err error) {
return nil, errors.New("Not supported")
}
func (k *ecdsaPrivateKey) SKI() (ski []byte) {
if k.privKey == nil {
return nil
}
raw := elliptic.Marshal(k.privKey.Curve, k.privKey.PublicKey.X, k.privKey.PublicKey.Y)
hash := sha256.New()
hash.Write(raw)
return hash.Sum(nil)
}
func (k *ecdsaPrivateKey) Symmetric() bool {
return false
}
func (k *ecdsaPrivateKey) Private() bool {
return true
}
func (k *ecdsaPrivateKey) PublicKey() (Key, error) {
return &ecdsaPublicKey{&k.privKey.PublicKey}, nil
}
type ecdsaPublicKey struct {
pubKey *ecdsa.PublicKey
}
func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
raw, err = x509.MarshalPKIXPublicKey(k.pubKey)
if err != nil {
return nil, fmt.Errorf("Failed marshalling key [%s]", err)
}
return
}
func (k *ecdsaPublicKey) SKI() (ski []byte) {
if k.pubKey == nil {
return nil
}
raw := elliptic.Marshal(k.pubKey.Curve, k.pubKey.X, k.pubKey.Y)
hash := sha256.New()
hash.Write(raw)
return hash.Sum(nil)
}
func (k *ecdsaPublicKey) Symmetric() bool {
return false
}
func (k *ecdsaPublicKey) Private() bool {
return false
}
func (k *ecdsaPublicKey) PublicKey() (Key, error) {
return k, nil
}