diff --git a/cipher/cipher.go b/cipher/cipher.go index a9ce756..5f16434 100644 --- a/cipher/cipher.go +++ b/cipher/cipher.go @@ -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 diff --git a/crypto/aes_test.go b/crypto/aes_test.go index a1892ee..4d61949 100644 --- a/crypto/aes_test.go +++ b/crypto/aes_test.go @@ -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" diff --git a/crypto/des_test.go b/crypto/des_test.go index 7e07e4c..97a7fcb 100644 --- a/crypto/des_test.go +++ b/crypto/des_test.go @@ -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) { diff --git a/crypto/factory.go b/crypto/factory.go index f54b841..06a0d21 100644 --- a/crypto/factory.go +++ b/crypto/factory.go @@ -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" ) /* diff --git a/crypto/rsa_test.go b/crypto/rsa_test.go index 3c01087..549d0f8 100644 --- a/crypto/rsa_test.go +++ b/crypto/rsa_test.go @@ -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) { diff --git a/rsa/cipher.go b/rsa/cipher.go index f13f1a9..67fde57 100644 --- a/rsa/cipher.go +++ b/rsa/cipher.go @@ -5,7 +5,7 @@ import ( "crypto" "errors" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) type Cipher interface { diff --git a/rsa/key.go b/rsa/key.go index 95fa319..e0edef8 100644 --- a/rsa/key.go +++ b/rsa/key.go @@ -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 断言如果入参为"" ,则直接报错 @@ -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 diff --git a/rsa/mode.go b/rsa/mode.go index b14e614..44856dc 100644 --- a/rsa/mode.go +++ b/rsa/mode.go @@ -4,7 +4,9 @@ import ( "crypto" "crypto/rand" "crypto/rsa" + "crypto/sha1" "errors" + "hash" ) type CipherMode interface { @@ -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)) +}