Skip to content

Commit

Permalink
完善 openssh 子库
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jan 13, 2025
1 parent ef4e256 commit 941bbc9
Show file tree
Hide file tree
Showing 31 changed files with 818 additions and 277 deletions.
7 changes: 1 addition & 6 deletions cryptobin/bign/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package bign

type (
// 错误方法
EcgdsaErrorFunc = func([]error)
)

// 引出错误信息
func (this Bign) OnError(fn EcgdsaErrorFunc) Bign {
func (this Bign) OnError(fn func([]error)) Bign {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/bip0340/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package bip0340

type (
// 错误方法
Bip0340ErrorFunc = func([]error)
)

// 引出错误信息
func (this BIP0340) OnError(fn Bip0340ErrorFunc) BIP0340 {
func (this BIP0340) OnError(fn func([]error)) BIP0340 {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/ca/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package ca

type (
// 错误方法
CAErrorFunc = func([]error)
)

// 引出错误信息
func (this CA) OnError(fn CAErrorFunc) CA {
func (this CA) OnError(fn func([]error)) CA {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/dh/curve25519/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package curve25519

type (
// 错误方法
ErrorFunc = func([]error)
)

// 引出错误信息
func (this Curve25519) OnError(fn ErrorFunc) Curve25519 {
func (this Curve25519) OnError(fn func([]error)) Curve25519 {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/dh/dh/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package dh

type (
// 错误方法
ErrorFunc = func([]error)
)

// 引出错误信息
func (this DH) OnError(fn ErrorFunc) DH {
func (this DH) OnError(fn func([]error)) DH {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/dh/ecdh/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package ecdh

type (
// 错误方法
ErrorFunc = func([]error)
)

// 引出错误信息
func (this ECDH) OnError(fn ErrorFunc) ECDH {
func (this ECDH) OnError(fn func([]error)) ECDH {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/dsa/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package dsa

type (
// 错误方法
DSAErrorFunc = func([]error)
)

// 引出错误信息
func (this DSA) OnError(fn DSAErrorFunc) DSA {
func (this DSA) OnError(fn func([]error)) DSA {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/ecdh/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package ecdh

type (
// 错误方法
ErrorFunc = func([]error)
)

// 引出错误信息
func (this ECDH) OnError(fn ErrorFunc) ECDH {
func (this ECDH) OnError(fn func([]error)) ECDH {
fn(this.Errors)

return this
Expand Down
16 changes: 8 additions & 8 deletions cryptobin/ecdsa/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (this ECDSA) CreatePKCS1PrivateKey() ECDSA {
return this.AppendError(err)
}

publicKeyBytes, err := x509.MarshalECPrivateKey(this.privateKey)
privateKeyBytes, err := x509.MarshalECPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: publicKeyBytes,
Bytes: privateKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand Down Expand Up @@ -85,7 +85,7 @@ func (this ECDSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...str
}

// 生成私钥
publicKeyBytes, err := x509.MarshalECPrivateKey(this.privateKey)
privateKeyBytes, err := x509.MarshalECPrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -94,7 +94,7 @@ func (this ECDSA) CreatePKCS1PrivateKeyWithPassword(password string, opts ...str
privateBlock, err := pkcs1.EncryptPEMBlock(
rand.Reader,
"EC PRIVATE KEY",
publicKeyBytes,
privateKeyBytes,
[]byte(password),
cipher,
)
Expand All @@ -116,14 +116,14 @@ func (this ECDSA) CreatePKCS8PrivateKey() ECDSA {
return this.AppendError(err)
}

publicKeyBytes, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: publicKeyBytes,
Bytes: privateKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand All @@ -145,7 +145,7 @@ func (this ECDSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any
}

// 生成私钥
publicKeyBytes, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(this.privateKey)
if err != nil {
return this.AppendError(err)
}
Expand All @@ -154,7 +154,7 @@ func (this ECDSA) CreatePKCS8PrivateKeyWithPassword(password string, opts ...any
privateBlock, err := pkcs8.EncryptPEMBlock(
rand.Reader,
"ENCRYPTED PRIVATE KEY",
publicKeyBytes,
privateKeyBytes,
[]byte(password),
opt,
)
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/ecdsa/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package ecdsa

type (
// 错误方法
EcdsaErrorFunc = func([]error)
)

// 引出错误信息
func (this ECDSA) OnError(fn EcdsaErrorFunc) ECDSA {
func (this ECDSA) OnError(fn func([]error)) ECDSA {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/ecgdsa/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package ecgdsa

type (
// 错误方法
EcgdsaErrorFunc = func([]error)
)

// 引出错误信息
func (this ECGDSA) OnError(fn EcgdsaErrorFunc) ECGDSA {
func (this ECGDSA) OnError(fn func([]error)) ECGDSA {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/ecsdsa/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package ecsdsa

type (
// 错误方法
EcgdsaErrorFunc = func([]error)
)

// 引出错误信息
func (this ECSDSA) OnError(fn EcgdsaErrorFunc) ECSDSA {
func (this ECSDSA) OnError(fn func([]error)) ECSDSA {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/ed448/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package ed448

type (
// 错误方法
EdDSAErrorFunc = func([]error)
)

// 引出错误信息
func (this ED448) OnError(fn EdDSAErrorFunc) ED448 {
func (this ED448) OnError(fn func([]error)) ED448 {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/eddsa/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package eddsa

type (
// 错误方法
EdDSAErrorFunc = func([]error)
)

// 引出错误信息
func (this EdDSA) OnError(fn EdDSAErrorFunc) EdDSA {
func (this EdDSA) OnError(fn func([]error)) EdDSA {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/elgamal/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package elgamal

type (
// 错误方法
DSAErrorFunc = func([]error)
)

// 引出错误信息
func (this ElGamal) OnError(fn DSAErrorFunc) ElGamal {
func (this ElGamal) OnError(fn func([]error)) ElGamal {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/gost/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package gost

type (
// 错误方法
DSAErrorFunc = func([]error)
)

// 引出错误信息
func (this Gost) OnError(fn DSAErrorFunc) Gost {
func (this Gost) OnError(fn func([]error)) Gost {
fn(this.Errors)

return this
Expand Down
7 changes: 1 addition & 6 deletions cryptobin/rsa/on.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package rsa

type (
// 错误方法
RsaErrorFunc = func([]error)
)

// 引出错误信息
func (this RSA) OnError(fn RsaErrorFunc) RSA {
func (this RSA) OnError(fn func([]error)) RSA {
fn(this.Errors)

return this
Expand Down
8 changes: 4 additions & 4 deletions cryptobin/ssh/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"reflect"
)

type publicKeyEqual interface {
type keyEqual interface {
Equal(x crypto.PublicKey) bool
}

// 检测公钥私钥是否匹配
// Check Key Pair
func (this SSH) CheckKeyPair() bool {
// 私钥导出的公钥
// get publicKey from privateKey
pubKeyFromPriKey := this.MakePublicKey().publicKey

if pubKeyFromPriKey == nil || this.publicKey == nil {
return false
}

if pubkeyEqual, ok := pubKeyFromPriKey.(publicKeyEqual); ok {
if pubkeyEqual, ok := pubKeyFromPriKey.(keyEqual); ok {
if pubkeyEqual.Equal(this.publicKey) {
return true
}
Expand Down
Loading

0 comments on commit 941bbc9

Please sign in to comment.