mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-16 10:42:27 +08:00
refactor: add error value return for GenerateRsaKey func
This commit is contained in:
@@ -147,7 +147,7 @@ func Md5File(filename string) (string, error) //return the md5 value of file
|
|||||||
func Sha1(data string) string //get sha1 value
|
func Sha1(data string) string //get sha1 value
|
||||||
func Sha256(data string) string //getsha256 value
|
func Sha256(data string) string //getsha256 value
|
||||||
func Sha512(data string) string //get sha512 value
|
func Sha512(data string) string //get sha512 value
|
||||||
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) //generate RSA pem file
|
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error //generate RSA pem file
|
||||||
func RsaEncrypt(data []byte, pubKeyFileName string) []byte //RSA encrypt
|
func RsaEncrypt(data []byte, pubKeyFileName string) []byte //RSA encrypt
|
||||||
func RsaDecrypt(data []byte, privateKeyFileName string) []byte //RSA decrypt
|
func RsaDecrypt(data []byte, privateKeyFileName string) []byte //RSA decrypt
|
||||||
|
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ func Md5File(filename string) (string, error) //获取文件md5值
|
|||||||
func Sha1(data string) string //获取sha1值
|
func Sha1(data string) string //获取sha1值
|
||||||
func Sha256(data string) string //获取sha256值
|
func Sha256(data string) string //获取sha256值
|
||||||
func Sha512(data string) string //获取sha512值
|
func Sha512(data string) string //获取sha512值
|
||||||
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) //生成RSA私钥文件
|
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error //生成RSA私钥文件
|
||||||
func RsaEncrypt(data []byte, pubKeyFileName string) []byte //RSA加密
|
func RsaEncrypt(data []byte, pubKeyFileName string) []byte //RSA加密
|
||||||
func RsaDecrypt(data []byte, privateKeyFileName string) []byte //RSA解密
|
func RsaDecrypt(data []byte, privateKeyFileName string) []byte //RSA解密
|
||||||
|
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ import (
|
|||||||
|
|
||||||
// GenerateRsaKey make a rsa private key, and return key file name
|
// GenerateRsaKey make a rsa private key, and return key file name
|
||||||
// Generated key file is `rsa_private.pem` and `rsa_public.pem` in current path
|
// Generated key file is `rsa_private.pem` and `rsa_public.pem` in current path
|
||||||
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) {
|
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error {
|
||||||
// private key
|
// private key
|
||||||
privateKey, err := rsa.GenerateKey(rand.Reader, keySize)
|
privateKey, err := rsa.GenerateKey(rand.Reader, keySize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
derText := x509.MarshalPKCS1PrivateKey(privateKey)
|
derText := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||||
@@ -41,7 +41,7 @@ func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) {
|
|||||||
|
|
||||||
derpText, err := x509.MarshalPKIXPublicKey(&publicKey)
|
derpText, err := x509.MarshalPKIXPublicKey(&publicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
block = pem.Block{
|
block = pem.Block{
|
||||||
@@ -52,10 +52,12 @@ func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) {
|
|||||||
//file,err = os.Create("rsa_public.pem")
|
//file,err = os.Create("rsa_public.pem")
|
||||||
file, err = os.Create(pubKeyFile)
|
file, err = os.Create(pubKeyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
pem.Encode(file, &block)
|
pem.Encode(file, &block)
|
||||||
file.Close()
|
file.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RsaEncrypt encrypt data with ras algorithm
|
// RsaEncrypt encrypt data with ras algorithm
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestRsaEncrypt(t *testing.T) {
|
func TestRsaEncrypt(t *testing.T) {
|
||||||
GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := RsaEncrypt(data, "rsa_public.pem")
|
encrypted := RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
|
|||||||
Reference in New Issue
Block a user