@@ -22,21 +22,21 @@ var (
2222 oidPublicKeyGmSM2 = asn1.ObjectIdentifier {1 , 2 , 156 , 10197 , 1 , 301 }
2323)
2424
25- // 私钥 - 包装
25+ // PrivateKey info
2626type pkcs8 struct {
2727 Version int
2828 Algo pkix.AlgorithmIdentifier
2929 PrivateKey []byte
3030 Attributes []asn1.RawValue `asn1:"optional,tag:0"`
3131}
3232
33- // 公钥 - 包装
33+ // pkix PublicKey info
3434type pkixPublicKey struct {
3535 Algo pkix.AlgorithmIdentifier
3636 BitString asn1.BitString
3737}
3838
39- // 公钥信息 - 解析
39+ // publicKey Info
4040type publicKeyInfo struct {
4141 Raw asn1.RawContent
4242 Algorithm pkix.AlgorithmIdentifier
@@ -50,7 +50,7 @@ type ecPrivateKey struct {
5050 PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
5151}
5252
53- // 包装公钥
53+ // Marshal PublicKey
5454func MarshalPublicKey (pub * PublicKey ) ([]byte , error ) {
5555 switch pub .Curve () {
5656 case X448 ():
@@ -79,55 +79,53 @@ func MarshalPublicKey(pub *PublicKey) ([]byte, error) {
7979
8080 pubkey , err := sm2 .MarshalPublicKey (pubblic )
8181 if err != nil {
82- return nil , errors .New ("ecdsa : failed to marshal algo param: " + err .Error ())
82+ return nil , errors .New ("go-cryptobin/ecdh : failed to marshal algo param: " + err .Error ())
8383 }
8484
8585 return pubkey , nil
8686 default :
8787 pubkey , err := ToPublicKey (pub )
8888 if err != nil {
89- return nil , fmt .Errorf ("ecdh: failed to marshal public key: %v" , err )
89+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: failed to marshal public key: %v" , err )
9090 }
9191
9292 public , err := x509 .MarshalPKIXPublicKey (pubkey )
9393 if err != nil {
94- return nil , fmt .Errorf ("ecdh: failed to marshal public key: %v" , err )
94+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: failed to marshal public key: %v" , err )
9595 }
9696
9797 return public , nil
9898 }
9999}
100100
101- // 解析公钥
101+ // Parse PublicKey
102102func ParsePublicKey (derBytes []byte ) (* PublicKey , error ) {
103103 var pki publicKeyInfo
104104 if rest , err := asn1 .Unmarshal (derBytes , & pki ); err != nil {
105105 return nil , err
106106 } else if len (rest ) != 0 {
107- return nil , errors .New ("ecdh: trailing data after ASN.1 of public-key" )
107+ return nil , errors .New ("go-cryptobin/ ecdh: trailing data after ASN.1 of public-key" )
108108 }
109109
110- keyData := & pki
111-
112- oid := keyData .Algorithm .Algorithm
113- params := keyData .Algorithm .Parameters
114- der := cryptobyte .String (keyData .PublicKey .RightAlign ())
110+ oid := pki .Algorithm .Algorithm
111+ params := pki .Algorithm .Parameters
112+ der := cryptobyte .String (pki .PublicKey .RightAlign ())
115113
116114 switch {
117115 case oid .Equal (oidPublicKeyX448 ):
118116 if len (params .FullBytes ) != 0 {
119- return nil , errors .New ("ecdh: X25519 key encoded with illegal parameters" )
117+ return nil , errors .New ("go-cryptobin/ ecdh: X25519 key encoded with illegal parameters" )
120118 }
121119
122120 return X448 ().NewPublicKey (der )
123121 default :
124- // 先判断 SM2 证书
122+ // check SM2 cert
125123 if oid .Equal (oidPublicKeyECDSA ) {
126124 paramsDer := cryptobyte .String (params .FullBytes )
127125
128126 namedCurveOID := new (asn1.ObjectIdentifier )
129127 if ! paramsDer .ReadASN1ObjectIdentifier (namedCurveOID ) {
130- return nil , errors .New ("ecdh: invalid ECDH parameters" )
128+ return nil , errors .New ("go-cryptobin/ ecdh: invalid ECDH parameters" )
131129 }
132130
133131 if oidPublicKeyGmSM2 .Equal (* namedCurveOID ) {
@@ -137,7 +135,7 @@ func ParsePublicKey(derBytes []byte) (*PublicKey, error) {
137135 }
138136 }
139137
140- // 其他 EC 曲线
138+ // other EC curve
141139 key , err := x509 .ParsePKIXPublicKey (derBytes )
142140 if err != nil {
143141 return nil , err
@@ -158,14 +156,14 @@ func ParsePublicKey(derBytes []byte) (*PublicKey, error) {
158156 }
159157 }
160158
161- return nil , errors .New ("ecdh: unknown public key algorithm" )
159+ return nil , errors .New ("go-cryptobin/ ecdh: unknown public key algorithm" )
162160 }
163161
164162}
165163
166164// ====================
167165
168- // 包装私钥
166+ // Marshal PrivateKey
169167func MarshalPrivateKey (key * PrivateKey ) ([]byte , error ) {
170168 var privKey pkcs8
171169
@@ -176,7 +174,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
176174 }
177175 var err error
178176 if privKey .PrivateKey , err = asn1 .Marshal (key .Bytes ()); err != nil {
179- return nil , fmt .Errorf ("ecdh: failed to marshal private key: %v" , err )
177+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: failed to marshal private key: %v" , err )
180178 }
181179 case GmSM2 ():
182180 c := sm2 .P256 ()
@@ -190,19 +188,19 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
190188
191189 private , err := sm2 .MarshalPrivateKey (pri )
192190 if err != nil {
193- return nil , errors .New ("ecdsa : failed to marshal algo param: " + err .Error ())
191+ return nil , errors .New ("go-cryptobin/ecdh : failed to marshal algo param: " + err .Error ())
194192 }
195193
196194 return private , nil
197195 default :
198196 prikey , err := ToPrivateKey (key )
199197 if err != nil {
200- return nil , fmt .Errorf ("ecdh: failed to marshal private key: %v" , err )
198+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: failed to marshal private key: %v" , err )
201199 }
202200
203201 private , err := x509 .MarshalPKCS8PrivateKey (prikey )
204202 if err != nil {
205- return nil , fmt .Errorf ("ecdh: failed to marshal private key: %v" , err )
203+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: failed to marshal private key: %v" , err )
206204 }
207205
208206 return private , nil
@@ -211,7 +209,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
211209 return asn1 .Marshal (privKey )
212210}
213211
214- // 解析私钥
212+ // Parse PrivateKey
215213func ParsePrivateKey (der []byte ) (* PrivateKey , error ) {
216214 var privKey pkcs8
217215 if _ , err := asn1 .Unmarshal (der , & privKey ); err != nil {
@@ -221,17 +219,17 @@ func ParsePrivateKey(der []byte) (*PrivateKey, error) {
221219 switch {
222220 case privKey .Algo .Algorithm .Equal (oidPublicKeyX448 ):
223221 if l := len (privKey .Algo .Parameters .FullBytes ); l != 0 {
224- return nil , errors .New ("ecdh: invalid X448 private key parameters" )
222+ return nil , errors .New ("go-cryptobin/ ecdh: invalid X448 private key parameters" )
225223 }
226224
227225 var curvePrivateKey []byte
228226 if _ , err := asn1 .Unmarshal (privKey .PrivateKey , & curvePrivateKey ); err != nil {
229- return nil , fmt .Errorf ("ecdh: invalid X448 private key: %v" , err )
227+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: invalid X448 private key: %v" , err )
230228 }
231229
232230 return X448 ().NewPrivateKey (curvePrivateKey )
233231 default :
234- // 先判断 SM2 证书
232+ // check SM2 cert
235233 if privKey .Algo .Algorithm .Equal (oidPublicKeyECDSA ) {
236234 bytes := privKey .Algo .Parameters .FullBytes
237235
@@ -247,7 +245,7 @@ func ParsePrivateKey(der []byte) (*PrivateKey, error) {
247245 }
248246 }
249247
250- // 其他 EC 曲线
248+ // other EC curve
251249 key , err := x509 .ParsePKCS8PrivateKey (der )
252250 if err != nil {
253251 return nil , err
@@ -267,13 +265,12 @@ func ParsePrivateKey(der []byte) (*PrivateKey, error) {
267265 }
268266 }
269267
270- return nil , fmt .Errorf ("ecdh: PKCS#8 wrapping contained private key with unknown algorithm: %v" , privKey .Algo .Algorithm )
268+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: PKCS#8 wrapping contained private key with unknown algorithm: %v" , privKey .Algo .Algorithm )
271269 }
272270}
273271
274272// ================
275273
276- // 格式转换
277274func FromPrivateKey (key * crypto_ecdh.PrivateKey ) (* PrivateKey , error ) {
278275 switch key .Curve () {
279276 case crypto_ecdh .P256 ():
@@ -286,7 +283,7 @@ func FromPrivateKey(key *crypto_ecdh.PrivateKey) (*PrivateKey, error) {
286283 return X25519 ().NewPrivateKey (key .Bytes ())
287284 }
288285
289- return nil , fmt .Errorf ("ecdh: PrivateKey is not support" )
286+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: PrivateKey is not support" )
290287}
291288
292289func ToPrivateKey (key * PrivateKey ) (* crypto_ecdh.PrivateKey , error ) {
@@ -302,12 +299,12 @@ func ToPrivateKey(key *PrivateKey) (*crypto_ecdh.PrivateKey, error) {
302299 case X25519 ():
303300 newCurve = crypto_ecdh .X25519 ()
304301 default :
305- return nil , fmt .Errorf ("ecdh: unknown key type while marshaling PKCS#8: %T" , key )
302+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: unknown key type while marshaling PKCS#8: %T" , key )
306303 }
307304
308305 prikey , err := newCurve .NewPrivateKey (key .Bytes ())
309306 if err != nil {
310- return nil , fmt .Errorf ("ecdh: failed to marshal private key: %v" , err )
307+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: failed to marshal private key: %v" , err )
311308 }
312309
313310 return prikey , nil
@@ -325,7 +322,7 @@ func FromPublicKey(pub *crypto_ecdh.PublicKey) (*PublicKey, error) {
325322 return X25519 ().NewPublicKey (pub .Bytes ())
326323 }
327324
328- return nil , fmt .Errorf ("ecdh: PublicKey is not support" )
325+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: PublicKey is not support" )
329326}
330327
331328func ToPublicKey (pub * PublicKey ) (* crypto_ecdh.PublicKey , error ) {
@@ -341,12 +338,12 @@ func ToPublicKey(pub *PublicKey) (*crypto_ecdh.PublicKey, error) {
341338 case X25519 ():
342339 newCurve = crypto_ecdh .X25519 ()
343340 default :
344- return nil , fmt .Errorf ("ecdh: unsupported public key type: %T" , pub )
341+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: unsupported public key type: %T" , pub )
345342 }
346343
347344 pubkey , err := newCurve .NewPublicKey (pub .Bytes ())
348345 if err != nil {
349- return nil , fmt .Errorf ("ecdh: failed to marshal public key: %v" , err )
346+ return nil , fmt .Errorf ("go-cryptobin/ ecdh: failed to marshal public key: %v" , err )
350347 }
351348
352349 return pubkey , nil
0 commit comments