github.com/turingchain2020/turingchain@v1.1.21/system/crypto/ed25519/ed25519.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package ed25519 ed25519系统加密包 6 package ed25519 7 8 import ( 9 "bytes" 10 "errors" 11 "fmt" 12 13 "github.com/turingchain2020/turingchain/common/crypto" 14 "github.com/turingchain2020/turingchain/system/crypto/ed25519/ed25519" 15 ) 16 17 //Driver 驱动 18 type Driver struct{} 19 20 //GenKey 生成私钥 21 func (d Driver) GenKey() (crypto.PrivKey, error) { 22 privKeyBytes := new([64]byte) 23 copy(privKeyBytes[:32], crypto.CRandBytes(32)) 24 ed25519.MakePublicKey(privKeyBytes) 25 return PrivKeyEd25519(*privKeyBytes), nil 26 } 27 28 //PrivKeyFromBytes 字节转为私钥 29 func (d Driver) PrivKeyFromBytes(b []byte) (privKey crypto.PrivKey, err error) { 30 if len(b) != 64 && len(b) != 32 { 31 return nil, errors.New("invalid priv key byte") 32 } 33 privKeyBytes := new([64]byte) 34 copy(privKeyBytes[:32], b[:32]) 35 ed25519.MakePublicKey(privKeyBytes) 36 return PrivKeyEd25519(*privKeyBytes), nil 37 } 38 39 //PubKeyFromBytes 字节转为公钥 40 func (d Driver) PubKeyFromBytes(b []byte) (pubKey crypto.PubKey, err error) { 41 if len(b) != 32 { 42 return nil, errors.New("invalid pub key byte") 43 } 44 pubKeyBytes := new([32]byte) 45 copy(pubKeyBytes[:], b[:]) 46 return PubKeyEd25519(*pubKeyBytes), nil 47 } 48 49 //SignatureFromBytes 字节转为签名 50 func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) { 51 sigBytes := new([64]byte) 52 copy(sigBytes[:], b[:]) 53 return SignatureEd25519(*sigBytes), nil 54 } 55 56 // Validate validate msg and signature 57 func (d Driver) Validate(msg, pub, sig []byte) error { 58 return crypto.BasicValidation(d, msg, pub, sig) 59 } 60 61 //PrivKeyEd25519 PrivKey 62 type PrivKeyEd25519 [64]byte 63 64 //Bytes 字节格式 65 func (privKey PrivKeyEd25519) Bytes() []byte { 66 s := make([]byte, 64) 67 copy(s, privKey[:]) 68 return s 69 } 70 71 //Sign 签名 72 func (privKey PrivKeyEd25519) Sign(msg []byte) crypto.Signature { 73 privKeyBytes := [64]byte(privKey) 74 signatureBytes := ed25519.Sign(&privKeyBytes, msg) 75 return SignatureEd25519(*signatureBytes) 76 } 77 78 //PubKey 公钥 79 func (privKey PrivKeyEd25519) PubKey() crypto.PubKey { 80 privKeyBytes := [64]byte(privKey) 81 return PubKeyEd25519(*ed25519.MakePublicKey(&privKeyBytes)) 82 } 83 84 //Equals 相等 85 func (privKey PrivKeyEd25519) Equals(other crypto.PrivKey) bool { 86 if otherEd, ok := other.(PrivKeyEd25519); ok { 87 return bytes.Equal(privKey[:], otherEd[:]) 88 } 89 return false 90 } 91 92 //PubKeyEd25519 PubKey 93 type PubKeyEd25519 [32]byte 94 95 //Bytes 字节格式 96 func (pubKey PubKeyEd25519) Bytes() []byte { 97 s := make([]byte, 32) 98 copy(s, pubKey[:]) 99 return s 100 } 101 102 //VerifyBytes 验证字节 103 func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig crypto.Signature) bool { 104 // unwrap if needed 105 if wrap, ok := sig.(SignatureS); ok { 106 sig = wrap.Signature 107 } 108 // make sure we use the same algorithm to sign 109 sigEd25519, ok := sig.(SignatureEd25519) 110 if !ok { 111 return false 112 } 113 pubKeyBytes := [32]byte(pubKey) 114 sigBytes := [64]byte(sigEd25519) 115 return ed25519.Verify(&pubKeyBytes, msg, &sigBytes) 116 } 117 118 //KeyString 公钥字符串格式 119 func (pubKey PubKeyEd25519) KeyString() string { 120 return fmt.Sprintf("%X", pubKey[:]) 121 } 122 123 //Equals 相等 124 func (pubKey PubKeyEd25519) Equals(other crypto.PubKey) bool { 125 if otherEd, ok := other.(PubKeyEd25519); ok { 126 return bytes.Equal(pubKey[:], otherEd[:]) 127 } 128 return false 129 } 130 131 //SignatureEd25519 Signature 132 type SignatureEd25519 [64]byte 133 134 //SignatureS 签名 135 type SignatureS struct { 136 crypto.Signature 137 } 138 139 //Bytes 字节格式 140 func (sig SignatureEd25519) Bytes() []byte { 141 s := make([]byte, 64) 142 copy(s, sig[:]) 143 return s 144 } 145 146 //IsZero 是否是0 147 func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 } 148 149 func (sig SignatureEd25519) String() string { 150 fingerprint := make([]byte, len(sig[:])) 151 copy(fingerprint, sig[:]) 152 return fmt.Sprintf("/%X.../", fingerprint) 153 } 154 155 //Equals 相等 156 func (sig SignatureEd25519) Equals(other crypto.Signature) bool { 157 if otherEd, ok := other.(SignatureEd25519); ok { 158 return bytes.Equal(sig[:], otherEd[:]) 159 } 160 return false 161 162 } 163 164 //const 165 const ( 166 Name = "ed25519" 167 ID = 2 168 ) 169 170 func init() { 171 crypto.Register(Name, &Driver{}, crypto.WithOptionTypeID(ID)) 172 }