Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct logrus #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cipher/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type blockCipher struct {
}

func (blockCipher *blockCipher) Encrypt(plaintext []byte) []byte {
plaintext = blockCipher.padding.Padding(plaintext, blockCipher.encrypt.BlockSize())
//TODO: modify By Eason ECB nopadding
//plaintext = blockCipher.padding.Padding(plaintext, blockCipher.encrypt.BlockSize())
ciphertext := make([]byte, len(plaintext))
blockCipher.encrypt.CryptBlocks(ciphertext, plaintext)
return ciphertext
Expand Down
4 changes: 2 additions & 2 deletions crypto/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"testing"

"github.com/89hmdys/toast/cipher"
"github.com/89hmdys/toast/crypto"
"github.com/QuanTran91/toast/cipher"
"github.com/QuanTran91/toast/crypto"
)

var key = "|-8xrgPfS)Aa4xtAIL^k5qX)$Y5Rim9Z"
Expand Down
4 changes: 2 additions & 2 deletions crypto/des_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"testing"

"github.com/89hmdys/toast/cipher"
"github.com/89hmdys/toast/crypto"
"github.com/QuanTran91/toast/cipher"
"github.com/QuanTran91/toast/crypto"
)

func Test_DES_ECB(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions crypto/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"crypto/aes"
"crypto/des"

. "github.com/89hmdys/toast/cipher"
"github.com/89hmdys/toast/rsa"
. "github.com/QuanTran91/toast/cipher"
"github.com/QuanTran91/toast/rsa"
)

/*
Expand Down
4 changes: 2 additions & 2 deletions crypto/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"testing"

"github.com/89hmdys/toast/crypto"
"github.com/89hmdys/toast/rsa"
"github.com/QuanTran91/toast/crypto"
"github.com/QuanTran91/toast/rsa"
)

func Test_LoadFromPEMFile(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion rsa/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto"

"errors"
"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)

type Cipher interface {
Expand Down
46 changes: 46 additions & 0 deletions rsa/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func ParsePKCS1Key(publicKey, privateKey []byte) (Key, error) {
return &key{publicKey: puk.(*rsa.PublicKey), privateKey: prk}, nil
}

func ParsePKCS1KeyByCert(publicKey, privateKey []byte) (Key, error) {
puk, err := x509.ParseCertificate(publicKey)
if err != nil {
return nil, err
}
prk, err := x509.ParsePKCS1PrivateKey(privateKey)
if err != nil {
return nil, err
}
return &key{publicKey: puk.PublicKey.(*rsa.PublicKey), privateKey: prk}, nil
}

func LoadKeyFromPEMFile(publicKeyFilePath, privateKeyFilePath string, ParseKey func([]byte, []byte) (Key, error)) (Key, error) {

//TODO 断言如果入参为"" ,则直接报错
Expand Down Expand Up @@ -71,6 +83,40 @@ func LoadKeyFromPEMFile(publicKeyFilePath, privateKeyFilePath string, ParseKey f
return ParseKey(puk.Bytes, prk.Bytes)
}

//Eason Lin
func LoadKeyFromPEMByte(pukBytes, prkBytes []byte, ParseKey func([]byte, []byte) (Key, error)) (Key, error) {
puk, _ := pem.Decode(pukBytes)
if puk == nil {
return nil, errors.New("publicKey is not pem formate")
}

prk, _ := pem.Decode(prkBytes)
if prk == nil {
return nil, errors.New("privateKey is not pem formate")
}

return ParseKey(puk.Bytes, prk.Bytes)
}

func LoadKeyFromDerFile(publicKeyFilePath, privateKeyFilePath string, ParseKey func([]byte, []byte) (Key, error)) (Key, error) {

publicKeyFilePath = strings.TrimSpace(publicKeyFilePath)

pukBytes, err := ioutil.ReadFile(publicKeyFilePath)
if err != nil {
return nil, err
}

privateKeyFilePath = strings.TrimSpace(privateKeyFilePath)

prkBytes, err := ioutil.ReadFile(privateKeyFilePath)
if err != nil {
return nil, err
}

return ParseKey(pukBytes, prkBytes)
}

type key struct {
publicKey *rsa.PublicKey
privateKey *rsa.PrivateKey
Expand Down
32 changes: 17 additions & 15 deletions rsa/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"errors"
"hash"
)

type CipherMode interface {
Expand Down Expand Up @@ -64,18 +66,18 @@ func (pkcs1v15 *pkcs1v15Sign) Verify(src []byte, sign []byte, hash crypto.Hash,
return rsa.VerifyPKCS1v15(puk, hash, hashed, sign)
}

//type oaepCipher struct {
// h hash.Hash
//}
//
//func NewOAEPCipher() CipherMode {
// return new(oaepCipher)
//}
//
//func (oaep *oaepCipher) Encrypt(plainText []byte, puk *rsa.PublicKey) ([]byte, error) {
// return rsa.EncryptOAEP(oaep.h, rand.Reader, puk, plainText, make([]byte, 0))
//}
//
//func (oaep *oaepCipher) Decrypt(cipherText []byte, prk *rsa.PrivateKey) ([]byte, error) {
// return rsa.DecryptOAEP(oaep.h, rand.Reader, prk, cipherText, make([]byte, 0))
//}
type oaepCipher struct {
h hash.Hash
}

func NewOAEPCipher() CipherMode {
return &oaepCipher{h: sha1.New()}
}

func (oaep *oaepCipher) Encrypt(plainText []byte, puk *rsa.PublicKey) ([]byte, error) {
return rsa.EncryptOAEP(oaep.h, rand.Reader, puk, plainText, make([]byte, 0))
}

func (oaep *oaepCipher) Decrypt(cipherText []byte, prk *rsa.PrivateKey) ([]byte, error) {
return rsa.DecryptOAEP(oaep.h, rand.Reader, prk, cipherText, make([]byte, 0))
}