goint there
This commit is contained in:
1 parent
776c73b91c
commit
c94a2042f1
7 files changed
+292
-36
No files matched your search
+104
-16
@@ -2,6 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
@@ -12,21 +15,17 @@ const (
|
||||
)
|
||||
|
||||
type PrivateKey struct {
|
||||
Name string
|
||||
Description string
|
||||
Type string // rsa, el gamal,...
|
||||
Version string
|
||||
Key interface{}
|
||||
Accept *AcceptMethods
|
||||
Version string
|
||||
Type string // rsa, el gamal,...
|
||||
Key *RSAPrivateKey
|
||||
Accept *AcceptMethods
|
||||
}
|
||||
|
||||
type PublicKey struct {
|
||||
Name string
|
||||
Description string
|
||||
Type string // rsa, el gamal,...
|
||||
Version string
|
||||
Key interface{}
|
||||
Accept *AcceptMethods
|
||||
Type string // rsa, el gamal,...
|
||||
Version string
|
||||
Key *RSAPublicKey
|
||||
Accept *AcceptMethods
|
||||
}
|
||||
|
||||
type RSAPublicKey struct {
|
||||
@@ -35,13 +34,102 @@ type RSAPublicKey struct {
|
||||
}
|
||||
|
||||
type RSAPrivateKey struct {
|
||||
N *big.Int
|
||||
P *big.Int
|
||||
Q *big.Int
|
||||
D *big.Int
|
||||
N *big.Int `json:"n,omitempty"`
|
||||
P *big.Int `json:"p,omitempty"`
|
||||
Q *big.Int `json:"q,omitempty"`
|
||||
D *big.Int `json:"d,omitempty"`
|
||||
E int `json:"e,omitempty"`
|
||||
}
|
||||
|
||||
type encodingRSAKey struct {
|
||||
N string
|
||||
P string
|
||||
Q string
|
||||
D string
|
||||
E int
|
||||
}
|
||||
|
||||
func (this *encodingRSAKey) CreateFromRSAPrivateKey(key *RSAPrivateKey) *encodingRSAKey {
|
||||
this.N = fmt.Sprintf("%x", key.N)
|
||||
this.P = fmt.Sprintf("%x", key.P)
|
||||
this.Q = fmt.Sprintf("%x", key.Q)
|
||||
this.D = fmt.Sprintf("%x", key.D)
|
||||
this.E = key.E
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *encodingRSAKey) CreateFromRSAPublicKey(key *RSAPublicKey) *encodingRSAKey {
|
||||
this.N = fmt.Sprintf("%x", key.N)
|
||||
this.E = key.E
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *encodingRSAKey) SetRSAPrivateKey(key *RSAPrivateKey) (*RSAPrivateKey, error) {
|
||||
if i, ok := new(big.Int).SetString(this.N, 16); !ok {
|
||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||
} else {
|
||||
key.N = i
|
||||
}
|
||||
|
||||
if i, ok := new(big.Int).SetString(this.P, 16); !ok {
|
||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||
} else {
|
||||
key.P = i
|
||||
}
|
||||
|
||||
if i, ok := new(big.Int).SetString(this.Q, 16); !ok {
|
||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||
} else {
|
||||
key.Q = i
|
||||
}
|
||||
|
||||
if i, ok := new(big.Int).SetString(this.D, 16); !ok {
|
||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||
} else {
|
||||
key.D = i
|
||||
}
|
||||
key.E = this.E
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func (this *encodingRSAKey) SetRSAPublicKey(key *RSAPublicKey) (*RSAPublicKey, error) {
|
||||
if i, ok := new(big.Int).SetString(this.N, 16); !ok {
|
||||
return nil, errors.New("cannot parse string has hexadecimal string")
|
||||
} else {
|
||||
key.N = i
|
||||
}
|
||||
key.E = this.E
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func (this *RSAPrivateKey) MarshalJSON() ([]byte, error) {
|
||||
e := new(encodingRSAKey)
|
||||
return json.Marshal(e.CreateFromRSAPrivateKey(this))
|
||||
}
|
||||
|
||||
func (this *RSAPrivateKey) UnmarshalJSON(data []byte) error {
|
||||
obj := new(encodingRSAKey)
|
||||
if err := json.Unmarshal(data, obj); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := obj.SetRSAPrivateKey(this)
|
||||
return err
|
||||
}
|
||||
|
||||
func (this *RSAPublicKey) MarshalJSON() ([]byte, error) {
|
||||
e := new(encodingRSAKey)
|
||||
return json.Marshal(e.CreateFromRSAPublicKey(this))
|
||||
}
|
||||
|
||||
func (this *RSAPublicKey) UnmarshalJSON(data []byte) error {
|
||||
obj := new(encodingRSAKey)
|
||||
if err := json.Unmarshal(data, obj); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := obj.SetRSAPublicKey(this)
|
||||
return err
|
||||
}
|
||||
|
||||
type AcceptMethods struct {
|
||||
BlocCypher string
|
||||
BlockCypherMode string
|
||||
|
||||
Reference in new issue
Block a user