Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jun 23, 2023
1 parent c0d61cf commit 35671de
Show file tree
Hide file tree
Showing 42 changed files with 3,855 additions and 173 deletions.
6 changes: 5 additions & 1 deletion app/example/cmd/make_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ var MakeKeyCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// key.NewRsa().Make()

key.NewDSA().Make()
// key.NewDSA().Make()

// key.KeyCheck()

// key.NewEcdh().Make()

// key.ShowTorrent()

key.ShowBerP12()

// key.NewGoEcdh().Make()

fmt.Println("生成各种证书成功")
},
}
Expand Down
5 changes: 3 additions & 2 deletions app/example/controller/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/deatil/lakego-filesystem/filesystem"

"github.com/deatil/go-cryptobin/ssh"
_ "github.com/deatil/go-cryptobin/ber"
_ "github.com/deatil/go-cryptobin/ber/asn1"
_ "github.com/deatil/go-cryptobin/argon2"
_ "github.com/deatil/go-cryptobin/bencode"
_ "github.com/deatil/go-cryptobin/dh/dh"
Expand All @@ -37,6 +37,7 @@ import (
_ "github.com/deatil/go-cryptobin/pkcs8s"
_ "github.com/deatil/go-cryptobin/pkcs12"
_ "github.com/deatil/go-cryptobin/jceks"
_ "github.com/deatil/go-cryptobin/elgamal"
_ "github.com/deatil/go-cryptobin/cryptobin/ca"
_ "github.com/deatil/go-cryptobin/cryptobin/dsa"
// _ "github.com/deatil/go-cryptobin/cryptobin/ecdh"
Expand Down Expand Up @@ -406,7 +407,7 @@ func (this *Data) Error(ctx *gin.Context) {
ToBase64String()
obj2Pub, _ := fs.Get("./runtime/key/key-pem/rsa/2048/rsa-pkcs8-pbe-en-SHA1AndDES.pub")
obj2cyptde := obj2.
FromBase64String("Pok10M8e9u1WicbS08/IvoKChoYXfKbljcJYr6srL5TkaAJTYD4thgPDV/EzRvCqfJsQyDb0cOqM2kmwKDt5zl+Amf6TitTPKb9LxCCuKcz6VKHtoUZ+t4ENZM4y2bjRNjkChWdjjEb0kjoljWoaZ+zoWl+6QWRRug6NQJag78J3crqVA34iulsygC/sVEy/LKSJ76PBDx9srdqXpf03HiJgYUSso7YnZ3RT+AS13GgZy7BFZskrjIX2Qw64X8Ydtt5TrfMckjxf0QWdNSwmFxSeNh1Cn2gozG9sJl7yiELNiG0JqRDIOYQTpszj314W5CYEIa/y4eRTDmiNiKr3cA==").
FromBase64String("QiDWcgThaEoeg664xpH6sIMNCfSfI2d/xvseMoCKNFeVK/xTX7xG+kNxZyQ21wXW1ljvhidRL7A+7eZnHEizoNQY1EJUsgDDMfuQxox0UwVe3PJ1VCtTg9XczbwTi2NHSSzmg2Y2QhSnmNzWmZMDM4xGUc/gLL21ynUwpKs0zLWk4mBOeZHX80o8MCUSwue+YZXsxE+vrP60PlC9wg/qgrlyCySVZ5X4RNdPJ1dKW/9e3vrNx4goSTNn4olytaVQ9bFlL1fNI7mkHbF4qy3OcDdQvTbfj4DqFRAn2VImzWwwdi0kuYoHZJfdZeZ1xPDHJ/Q5WA2BKBBo7ra2TPbf8A==").
FromPKCS8PublicKey([]byte(obj2Pub)).
Verify([]byte("test-pass")).
ToVerify()
Expand Down
61 changes: 58 additions & 3 deletions app/example/key/key_goecdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (this GoEcdh) Make() {

this.pkcs8(obj, curve)
this.pkcs8En(obj, curve)

this.pkcs8ECDH(obj, curve)
this.pkcs8EnECDH(obj, curve)
}
}

Expand All @@ -49,7 +52,7 @@ func (this GoEcdh) pkcs8(obj cGoEcdh, name string) {
CreatePublicKey().
ToKeyString()

file := fmt.Sprintf("%s/%s/%s-pkcs8", this.path, name, this.name)
file := fmt.Sprintf("%s/go_ecdh/%s/%s-pkcs8", this.path, name, this.name)

this.fs.Put(file, priKey)
this.fs.Put(file + ".pub", pubKey)
Expand All @@ -66,7 +69,7 @@ func (this GoEcdh) pkcs8En(obj cGoEcdh, name string) {
CreatePublicKey().
ToKeyString()

file := fmt.Sprintf("%s/%s/%s-pkcs8-en-%s-%s", this.path, name, this.name, c, h)
file := fmt.Sprintf("%s/go_ecdh/%s/%s-pkcs8-en-%s-%s", this.path, name, this.name, c, h)

this.fs.Put(file, priKey)
this.fs.Put(file + ".pub", pubKey)
Expand All @@ -82,7 +85,59 @@ func (this GoEcdh) pkcs8En(obj cGoEcdh, name string) {
CreatePublicKey().
ToKeyString()

file := fmt.Sprintf("%s/%s/%s-pkcs8-pbe-en-%s", this.path, name, this.name, c2)
file := fmt.Sprintf("%s/go_ecdh/%s/%s-pkcs8-pbe-en-%s", this.path, name, this.name, c2)

this.fs.Put(file, priKey)
this.fs.Put(file + ".pub", pubKey)
}

}

// =================

func (this GoEcdh) pkcs8ECDH(obj cGoEcdh, name string) {
// 生成证书
priKey := obj.
CreateECDHPrivateKey().
ToKeyString()
pubKey := obj.
CreateECDHPublicKey().
ToKeyString()

file := fmt.Sprintf("%s/ecdh/%s/%s-pkcs8", this.path, name, this.name)

this.fs.Put(file, priKey)
this.fs.Put(file + ".pub", pubKey)
}

func (this GoEcdh) pkcs8EnECDH(obj cGoEcdh, name string) {
for _, c := range Pkcs8Ciphers {
for _, h := range Pkcs8Hashes {
// 生成证书
priKey := obj.
CreateECDHPrivateKeyWithPassword(this.pass, c, h).
ToKeyString()
pubKey := obj.
CreateECDHPublicKey().
ToKeyString()

file := fmt.Sprintf("%s/ecdh/%s/%s-pkcs8-en-%s-%s", this.path, name, this.name, c, h)

this.fs.Put(file, priKey)
this.fs.Put(file + ".pub", pubKey)
}
}

for _, c2 := range Pkcs8PbeCiphers {
// 生成证书
priKey := obj.
CreateECDHPrivateKeyWithPassword(this.pass, c2).
ToKeyString()
pubKey := obj.
CreateECDHPublicKey().
ToKeyString()

file := fmt.Sprintf("%s/ecdh/%s/%s-pkcs8-pbe-en-%s", this.path, name, this.name, c2)

this.fs.Put(file, priKey)
this.fs.Put(file + ".pub", pubKey)
Expand Down
48 changes: 21 additions & 27 deletions pkg/lakego-pkg/go-cryptobin/ber/asn1/ber_asn1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package asn1

import (
"bytes"
"encoding/asn1"
"encoding/hex"
"fmt"
"math"
Expand All @@ -13,12 +12,6 @@ import (
"time"
)

// Compatibility vars for ber_asn1_test.go
var (
NullRawValue = asn1.NullRawValue
NullBytes = asn1.NullBytes
)

type boolTest struct {
in []byte
ok bool
Expand Down Expand Up @@ -161,8 +154,8 @@ type bitStringTest struct {
var bitStringTestData = []bitStringTest{
{[]byte{}, false, []byte{}, 0},
{[]byte{0x00}, true, []byte{}, 0},
{[]byte{0x07, 0x00}, true, []byte{0x00}, 7},
{[]byte{0x07, 0x40}, true, []byte{0x40}, 7},
{[]byte{0x07, 0x00}, true, []byte{0x00}, 1},
{[]byte{0x07, 0x40}, false, []byte{0x40}, 7},
{[]byte{0x08, 0x00}, false, []byte{}, 0},
}

Expand All @@ -173,7 +166,7 @@ func TestBitString(t *testing.T) {
t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
}
if err == nil {
if test.bitLength != ret.PaddingBits || !bytes.Equal(ret.Bytes, test.out) {
if test.bitLength != ret.BitLength || !bytes.Equal(ret.Bytes, test.out) {
t.Errorf("#%d: Bad result: %v (expected %v %v)", i, ret, test.out, test.bitLength)
}
}
Expand Down Expand Up @@ -437,17 +430,17 @@ var unmarshalTestData = []struct {
out interface{}
}{
{[]byte{0x02, 0x01, 0x42}, newInt(0x42)},
{[]byte{0x05, 0x00}, &asn1.RawValue{0, 5, false, []byte{}, []byte{0x05, 0x00}}},
{[]byte{0x05, 0x00}, &RawValue{0, 5, false, false, []byte{}, []byte{0x05, 0x00}}},
{[]byte{0x30, 0x08, 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d}, &TestObjectIdentifierStruct{[]int{1, 2, 840, 113549}}},
{[]byte{0x03, 0x04, 0x06, 0x6e, 0x5d, 0xc0}, &BitString{[]byte{110, 93, 192}, 6}},
{[]byte{0x03, 0x04, 0x06, 0x6e, 0x5d, 0xc0}, &BitString{[]byte{110, 93, 192}, 18}},
{[]byte{0x30, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03}, &[]int{1, 2, 3}},
{[]byte{0x02, 0x01, 0x10}, newInt(16)},
{[]byte{0x13, 0x04, 't', 'e', 's', 't'}, newString("test")},
{[]byte{0x16, 0x04, 't', 'e', 's', 't'}, newString("test")},
// Ampersand is allowed in PrintableString due to mistakes by major CAs.
{[]byte{0x13, 0x05, 't', 'e', 's', 't', '&'}, newString("test&")},
{[]byte{0x16, 0x04, 't', 'e', 's', 't'}, &asn1.RawValue{0, 22, false, []byte("test"), []byte("\x16\x04test")}},
{[]byte{0x04, 0x04, 1, 2, 3, 4}, &asn1.RawValue{0, 4, false, []byte{1, 2, 3, 4}, []byte{4, 4, 1, 2, 3, 4}}},
{[]byte{0x16, 0x04, 't', 'e', 's', 't'}, &RawValue{0, 22, false, false, []byte("test"), []byte("\x16\x04test")}},
{[]byte{0x04, 0x04, 1, 2, 3, 4}, &RawValue{0, 4, false, false, []byte{1, 2, 3, 4}, []byte{4, 4, 1, 2, 3, 4}}},
{[]byte{0x30, 0x03, 0x81, 0x01, 0x01}, &TestContextSpecificTags{1}},
{[]byte{0x30, 0x08, 0xa1, 0x03, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02}, &TestContextSpecificTags2{1, 2}},
{[]byte{0x30, 0x03, 0x81, 0x01, '@'}, &TestContextSpecificTags3{"@"}},
Expand All @@ -467,6 +460,7 @@ func TestUnmarshal(t *testing.T) {
if err != nil {
t.Errorf("Unmarshal failed at index %d %v", i, err)
}

if !reflect.DeepEqual(val, test.out) {
t.Errorf("#%d:\nhave %#v\nwant %#v", i, val, test.out)
}
Expand All @@ -481,7 +475,7 @@ type Certificate struct {

type TBSCertificate struct {
Version int `asn1:"optional,explicit,default:0,tag:0"`
SerialNumber asn1.RawValue
SerialNumber RawValue
SignatureAlgorithm AlgorithmIdentifier
Issuer RDNSequence
Validity Validity
Expand Down Expand Up @@ -593,7 +587,7 @@ func TestObjectIdentifierEqual(t *testing.T) {
var derEncodedSelfSignedCert = Certificate{
TBSCertificate: TBSCertificate{
Version: 0,
SerialNumber: asn1.RawValue{Class: 0, Tag: 2, IsCompound: false, Bytes: []uint8{0x0, 0x8c, 0xc3, 0x37, 0x92, 0x10, 0xec, 0x2c, 0x98}, FullBytes: []byte{2, 9, 0x0, 0x8c, 0xc3, 0x37, 0x92, 0x10, 0xec, 0x2c, 0x98}},
SerialNumber: RawValue{Class: 0, Tag: 2, IsCompound: false, Bytes: []uint8{0x0, 0x8c, 0xc3, 0x37, 0x92, 0x10, 0xec, 0x2c, 0x98}, FullBytes: []byte{2, 9, 0x0, 0x8c, 0xc3, 0x37, 0x92, 0x10, 0xec, 0x2c, 0x98}},
SignatureAlgorithm: AlgorithmIdentifier{Algorithm: ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}},
Issuer: RDNSequence{
RelativeDistinguishedNameSET{AttributeTypeAndValue{Type: ObjectIdentifier{2, 5, 4, 6}, Value: "XX"}},
Expand Down Expand Up @@ -627,7 +621,7 @@ var derEncodedSelfSignedCert = Certificate{
0x2a, 0xf7, 0x58, 0x9c, 0xf2, 0xc7, 0x70, 0x45, 0xdc, 0x8f, 0xde, 0xec,
0x35, 0x7d, 0x2, 0x3, 0x1, 0x0, 0x1,
},
PaddingBits: 0,
BitLength: 592,
},
},
},
Expand All @@ -641,7 +635,7 @@ var derEncodedSelfSignedCert = Certificate{
0xd9, 0x1e, 0xde, 0x14, 0xa5, 0xed, 0x76, 0xbf, 0x11, 0x6f, 0xe3, 0x60, 0xaa,
0xfa, 0x88, 0x21, 0x49, 0x4, 0x35,
},
PaddingBits: 0,
BitLength: 512,
},
}

Expand Down Expand Up @@ -966,7 +960,7 @@ func TestUnexportedStructField(t *testing.T) {
}

func TestNull(t *testing.T) {
unmarshaled := asn1.RawValue{}
unmarshaled := RawValue{}
if _, err := Unmarshal(NullBytes, &unmarshaled); err != nil {
t.Fatal(err)
}
Expand All @@ -984,7 +978,7 @@ func TestNull(t *testing.T) {

func TestExplicitTagRawValueStruct(t *testing.T) {
type foo struct {
A asn1.RawValue `asn1:"optional,explicit,tag:5"`
A RawValue `asn1:"optional,explicit,tag:5"`
B []byte `asn1:"optional,explicit,tag:6"`
}
before := foo{B: []byte{1, 2, 3}}
Expand All @@ -1007,10 +1001,10 @@ func TestExplicitTagRawValueStruct(t *testing.T) {

func TestTaggedRawValue(t *testing.T) {
type taggedRawValue struct {
A asn1.RawValue `asn1:"tag:5"`
A RawValue `asn1:"tag:5"`
}
type untaggedRawValue struct {
A asn1.RawValue
A RawValue
}
const isCompound = 0x20
const tag = 5
Expand All @@ -1019,11 +1013,11 @@ func TestTaggedRawValue(t *testing.T) {
shouldMatch bool
derBytes []byte
}{
{false, []byte{0x30, 3, asn1.TagInteger, 1, 1}},
{true, []byte{0x30, 3, (asn1.ClassContextSpecific << 6) | tag, 1, 1}},
{true, []byte{0x30, 3, (asn1.ClassContextSpecific << 6) | tag | isCompound, 1, 1}},
{false, []byte{0x30, 3, (asn1.ClassApplication << 6) | tag | isCompound, 1, 1}},
{false, []byte{0x30, 3, (asn1.ClassPrivate << 6) | tag | isCompound, 1, 1}},
{false, []byte{0x30, 3, byte(TagInteger), 1, 1}},
{true, []byte{0x30, 3, (byte(TagClassContextSpecific) << 6) | tag, 1, 1}},
{true, []byte{0x30, 3, (byte(TagClassContextSpecific) << 6) | tag | isCompound, 1, 1}},
{false, []byte{0x30, 3, (byte(TagClassApplication) << 6) | tag | isCompound, 1, 1}},
{false, []byte{0x30, 3, (byte(TagClassPrivate) << 6) | tag | isCompound, 1, 1}},
}

for i, test := range tests {
Expand Down
Loading

0 comments on commit 35671de

Please sign in to comment.