|
| 1 | +package ecgdsa |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "crypto/rand" |
| 6 | + "crypto/rsa" |
| 7 | + "crypto/dsa" |
| 8 | + "crypto/ecdsa" |
| 9 | + "crypto/ed25519" |
| 10 | + |
| 11 | + "github.com/deatil/go-cryptobin/gm/sm2" |
| 12 | + "github.com/deatil/go-cryptobin/tool/encoding" |
| 13 | +) |
| 14 | + |
| 15 | +// 生成密钥 |
| 16 | +func (this SSH) GenerateKeyWithSeed(reader io.Reader) SSH { |
| 17 | + switch this.options.PublicKeyType { |
| 18 | + case KeyTypeRSA: |
| 19 | + privateKey, err := rsa.GenerateKey(reader, this.options.Bits) |
| 20 | + if err != nil { |
| 21 | + return this.AppendError(err) |
| 22 | + } |
| 23 | + |
| 24 | + this.privateKey = privateKey |
| 25 | + this.publicKey = &privateKey.PublicKey |
| 26 | + case KeyTypeDSA: |
| 27 | + privateKey := &dsa.PrivateKey{} |
| 28 | + dsa.GenerateParameters(&privateKey.Parameters, reader, this.options.ParameterSizes) |
| 29 | + dsa.GenerateKey(privateKey, reader) |
| 30 | + |
| 31 | + this.privateKey = privateKey |
| 32 | + this.publicKey = &privateKey.PublicKey |
| 33 | + case KeyTypeECDSA: |
| 34 | + privateKey, err := ecdsa.GenerateKey(this.options.Curve, reader) |
| 35 | + if err != nil { |
| 36 | + return this.AppendError(err) |
| 37 | + } |
| 38 | + |
| 39 | + this.privateKey = privateKey |
| 40 | + this.publicKey = &privateKey.PublicKey |
| 41 | + case KeyTypeEdDSA: |
| 42 | + publicKey, privateKey, err := ed25519.GenerateKey(reader) |
| 43 | + if err != nil { |
| 44 | + return this.AppendError(err) |
| 45 | + } |
| 46 | + |
| 47 | + this.privateKey = privateKey |
| 48 | + this.publicKey = publicKey |
| 49 | + case KeyTypeSM2: |
| 50 | + privateKey, err := sm2.GenerateKey(reader) |
| 51 | + if err != nil { |
| 52 | + return this.AppendError(err) |
| 53 | + } |
| 54 | + |
| 55 | + this.privateKey = privateKey |
| 56 | + this.publicKey = &privateKey.PublicKey |
| 57 | + } |
| 58 | + |
| 59 | + return this |
| 60 | +} |
| 61 | + |
| 62 | +// 使用自定义数据生成密钥对 |
| 63 | +func GenerateKeyWithSeed(reader io.Reader, options Options) SSH { |
| 64 | + return defaultSSH. |
| 65 | + WithOptions(options). |
| 66 | + GenerateKeyWithSeed(reader) |
| 67 | +} |
| 68 | + |
| 69 | +// 生成密钥 |
| 70 | +func (this SSH) GenerateKey() SSH { |
| 71 | + return this.GenerateKeyWithSeed(rand.Reader) |
| 72 | +} |
| 73 | + |
| 74 | +// 生成密钥对 |
| 75 | +func GenerateKey(options Options) SSH { |
| 76 | + return defaultSSH. |
| 77 | + WithOptions(options). |
| 78 | + GenerateKey() |
| 79 | +} |
| 80 | + |
| 81 | +// ========== |
| 82 | + |
| 83 | +// 私钥 |
| 84 | +func (this SSH) FromPrivateKey(key []byte) SSH { |
| 85 | + return this.FromOpensshPrivateKey(key) |
| 86 | +} |
| 87 | + |
| 88 | +// 私钥 |
| 89 | +func FromPrivateKey(key []byte) SSH { |
| 90 | + return defaultSSH.FromPrivateKey(key) |
| 91 | +} |
| 92 | + |
| 93 | +// 私钥带密码 |
| 94 | +func (this SSH) FromPrivateKeyWithPassword(key []byte, password []byte) SSH { |
| 95 | + return this.FromOpensshPrivateKeyWithPassword(key, password) |
| 96 | +} |
| 97 | + |
| 98 | +// 私钥带密码 |
| 99 | +func FromPrivateKeyWithPassword(key []byte, password []byte) SSH { |
| 100 | + return defaultSSH.FromPrivateKeyWithPassword(key, password) |
| 101 | +} |
| 102 | + |
| 103 | +// 公钥 |
| 104 | +func (this SSH) FromPublicKey(key []byte) SSH { |
| 105 | + return defaultSSH.FromOpensshPublicKey(key) |
| 106 | +} |
| 107 | + |
| 108 | +// 公钥 |
| 109 | +func FromPublicKey(key []byte) SSH { |
| 110 | + return defaultSSH.FromPublicKey(key) |
| 111 | +} |
| 112 | + |
| 113 | +// ========== |
| 114 | + |
| 115 | +// 私钥 |
| 116 | +func (this SSH) FromOpensshPrivateKey(key []byte) SSH { |
| 117 | + privateKey, comment, err := this.ParseOpensshPrivateKeyFromPEM(key) |
| 118 | + if err != nil { |
| 119 | + return this.AppendError(err) |
| 120 | + } |
| 121 | + |
| 122 | + this.privateKey = privateKey |
| 123 | + this.options.Comment = comment |
| 124 | + |
| 125 | + return this |
| 126 | +} |
| 127 | + |
| 128 | +// 私钥 |
| 129 | +func FromOpensshPrivateKey(key []byte) SSH { |
| 130 | + return defaultSSH.FromOpensshPrivateKey(key) |
| 131 | +} |
| 132 | + |
| 133 | +// 私钥带密码 |
| 134 | +func (this SSH) FromOpensshPrivateKeyWithPassword(key []byte, password []byte) SSH { |
| 135 | + privateKey, comment, err := this.ParseOpensshPrivateKeyFromPEMWithPassword(key, password) |
| 136 | + if err != nil { |
| 137 | + return this.AppendError(err) |
| 138 | + } |
| 139 | + |
| 140 | + this.privateKey = privateKey |
| 141 | + this.options.Comment = comment |
| 142 | + |
| 143 | + return this |
| 144 | +} |
| 145 | + |
| 146 | +// 私钥 |
| 147 | +func FromOpensshPrivateKeyWithPassword(key []byte, password []byte) SSH { |
| 148 | + return defaultSSH.FromOpensshPrivateKeyWithPassword(key, password) |
| 149 | +} |
| 150 | + |
| 151 | +// 公钥 |
| 152 | +func (this SSH) FromOpensshPublicKey(key []byte) SSH { |
| 153 | + publicKey, comment, err := this.ParseOpensshPublicKeyFromPEM(key) |
| 154 | + if err != nil { |
| 155 | + return this.AppendError(err) |
| 156 | + } |
| 157 | + |
| 158 | + this.publicKey = publicKey |
| 159 | + this.options.Comment = comment |
| 160 | + |
| 161 | + return this |
| 162 | +} |
| 163 | + |
| 164 | +// 公钥 |
| 165 | +func FromOpensshPublicKey(key []byte) SSH { |
| 166 | + return defaultSSH.FromOpensshPublicKey(key) |
| 167 | +} |
| 168 | + |
| 169 | +// ========== |
| 170 | + |
| 171 | +// 字节 |
| 172 | +func (this SSH) FromBytes(data []byte) SSH { |
| 173 | + this.data = data |
| 174 | + |
| 175 | + return this |
| 176 | +} |
| 177 | + |
| 178 | +// 字节 |
| 179 | +func FromBytes(data []byte) SSH { |
| 180 | + return defaultSSH.FromBytes(data) |
| 181 | +} |
| 182 | + |
| 183 | +// 字符 |
| 184 | +func (this SSH) FromString(data string) SSH { |
| 185 | + this.data = []byte(data) |
| 186 | + |
| 187 | + return this |
| 188 | +} |
| 189 | + |
| 190 | +// 字符 |
| 191 | +func FromString(data string) SSH { |
| 192 | + return defaultSSH.FromString(data) |
| 193 | +} |
| 194 | + |
| 195 | +// Base64 |
| 196 | +func (this SSH) FromBase64String(data string) SSH { |
| 197 | + newData, err := encoding.Base64Decode(data) |
| 198 | + |
| 199 | + this.data = newData |
| 200 | + |
| 201 | + return this.AppendError(err) |
| 202 | +} |
| 203 | + |
| 204 | +// Base64 |
| 205 | +func FromBase64String(data string) SSH { |
| 206 | + return defaultSSH.FromBase64String(data) |
| 207 | +} |
| 208 | + |
| 209 | +// Hex |
| 210 | +func (this SSH) FromHexString(data string) SSH { |
| 211 | + newData, err := encoding.HexDecode(data) |
| 212 | + |
| 213 | + this.data = newData |
| 214 | + |
| 215 | + return this.AppendError(err) |
| 216 | +} |
| 217 | + |
| 218 | +// Hex |
| 219 | +func FromHexString(data string) SSH { |
| 220 | + return defaultSSH.FromHexString(data) |
| 221 | +} |
0 commit comments