-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
643 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package crypto4go | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/md5" | ||
"crypto/rand" | ||
"golang.org/x/crypto/pbkdf2" | ||
"hash" | ||
) | ||
|
||
const ( | ||
K_PKCS5_SALT_LEN = 8 | ||
K_PKCS5_DEFAULT_ITER = 2048 | ||
K_PKCS5_DEFAULT_MAGIC = "Salted__" | ||
K_EVP_MAX_IV_LENGTH = 16 | ||
) | ||
|
||
func randBytes(length int) (data []byte, err error) { | ||
data = make([]byte, length) | ||
_, err = rand.Read(data) | ||
return data, err | ||
} | ||
|
||
func AESCBCEncryptWithSalt(plaintext, key []byte, iterCount int, magic string, h func() hash.Hash) ([]byte, error) { | ||
return AESEncryptWithSalt(plaintext, key, iterCount, magic, h, AESCBCEncrypt) | ||
} | ||
|
||
func AESCBCDecryptWithSalt(ciphertext, key []byte, iterCount int, magic string, h func() hash.Hash) ([]byte, error) { | ||
return AESDecryptWithSalt(ciphertext, key, iterCount, magic, h, AESCBCDecrypt) | ||
} | ||
|
||
func AESEncryptWithSalt(plaintext, key []byte, iterCount int, magic string, h func() hash.Hash, f func(plaintext, key, iv []byte) (dst []byte, err error)) (dst []byte, err error) { | ||
if iterCount <= 0 { | ||
iterCount = K_PKCS5_DEFAULT_ITER | ||
} | ||
|
||
if h == nil { | ||
h = md5.New | ||
} | ||
|
||
var salt, _ = randBytes(K_PKCS5_SALT_LEN) | ||
var sKey = pbkdf2.Key(key, salt, iterCount, len(key), h) | ||
var sIV = pbkdf2.Key(sKey, salt, iterCount, K_EVP_MAX_IV_LENGTH, h) | ||
|
||
dst, err = f(plaintext, sKey, sIV) | ||
|
||
dst = append(salt, dst...) | ||
dst = append([]byte(magic), dst...) | ||
|
||
return dst, err | ||
} | ||
|
||
func AESDecryptWithSalt(ciphertext, key []byte, iterCount int, magic string, h func() hash.Hash, f func(ciphertext, key, iv []byte) ([]byte, error)) (dst []byte, err error) { | ||
if iterCount <= 0 { | ||
iterCount = K_PKCS5_DEFAULT_ITER | ||
} | ||
|
||
if h == nil { | ||
h = md5.New | ||
} | ||
|
||
//if len(ciphertext) <= len(magic) + K_PKCS5_SALT_LEN { | ||
// return nil, errors.New("Error") | ||
//} | ||
|
||
var salt = ciphertext[len(magic) : len(magic)+K_PKCS5_SALT_LEN] | ||
var sKey = pbkdf2.Key(key, salt, iterCount, len(key), h) | ||
var sIV = pbkdf2.Key(sKey, salt, iterCount, K_EVP_MAX_IV_LENGTH, h) | ||
|
||
dst, err = f(ciphertext[len(magic)+K_PKCS5_SALT_LEN:], sKey, sIV) | ||
|
||
return dst, err | ||
} | ||
|
||
// AESCBCEncrypt 由key的长度决定是128, 192 还是 256 | ||
func AESCBCEncrypt(plaintext, key, iv []byte) ([]byte, error) { | ||
var block, err = aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var blockSize = block.BlockSize() | ||
iv = iv[:blockSize] | ||
|
||
var src = PKCS7Padding(plaintext, blockSize) | ||
var dst = make([]byte, len(src)) | ||
|
||
var mode = cipher.NewCBCEncrypter(block, iv) | ||
mode.CryptBlocks(dst, src) | ||
return dst, nil | ||
} | ||
|
||
func AESCBCDecrypt(ciphertext, key, iv []byte) ([]byte, error) { | ||
var block, err = aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var blockSize = block.BlockSize() | ||
iv = iv[:blockSize] | ||
|
||
var dst = make([]byte, len(ciphertext)) | ||
|
||
var mode = cipher.NewCBCDecrypter(block, iv) | ||
mode.CryptBlocks(dst, ciphertext) | ||
dst = PKCS7UnPadding(dst) | ||
return dst, nil | ||
} | ||
|
||
func AESCFBEncrypt(plaintext, key, iv []byte) ([]byte, error) { | ||
var block, err = aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var blockSize = block.BlockSize() | ||
iv = iv[:blockSize] | ||
|
||
var dst = make([]byte, len(plaintext)) | ||
|
||
var mode = cipher.NewCFBEncrypter(block, iv) | ||
mode.XORKeyStream(dst, plaintext) | ||
return dst, nil | ||
} | ||
|
||
func AESCFBDecrypt(ciphertext, key, iv []byte) ([]byte, error) { | ||
var block, err = aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var blockSize = block.BlockSize() | ||
iv = iv[:blockSize] | ||
|
||
var dst = make([]byte, len(ciphertext)) | ||
|
||
var mode = cipher.NewCFBDecrypter(block, iv) | ||
mode.XORKeyStream(dst, ciphertext) | ||
return dst, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package crypto4go | ||
|
||
//import ( | ||
// "testing" | ||
// "fmt" | ||
// "encoding/hex" | ||
//) | ||
// | ||
//var plain = "use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later." | ||
//var key = "11111111111111111111111111111111" | ||
//var iv = "1111111111111111" | ||
// | ||
//func Test_AES_CBC(t *testing.T) { | ||
// var a = []byte(plain) | ||
// var r, _ = AESCBCEncrypt(a, []byte(key), []byte(iv)) | ||
// fmt.Println("AES CBC Encrypt: ", hex.EncodeToString(r)) | ||
// | ||
// r, _ = AESCBCDecrypt(r, []byte(key), []byte(iv)) | ||
// fmt.Println("AES CBC Decrypt: ", string(r)) | ||
//} | ||
// | ||
//func Test_AES_CFB(t *testing.T) { | ||
// var a = []byte(plain) | ||
// var r, _ = AESCFBEncrypt(a, []byte(key), []byte(iv)) | ||
// fmt.Println("AES CFB Encrypt: ", hex.EncodeToString(r)) | ||
// | ||
// r, _ = AESCFBDecrypt(r, []byte(key), []byte(iv)) | ||
// fmt.Println("AES CFB Decrypt: ", string(r)) | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package crypto4go | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
) | ||
|
||
const ( | ||
kCertificatePrefix = "-----BEGIN CERTIFICATE-----" | ||
kCertificateSuffix = "-----END CERTIFICATE-----" | ||
) | ||
|
||
var ( | ||
ErrCertificateFailedToLoad = errors.New("crypto4go: certificate failed to load") | ||
) | ||
|
||
func FormatCertificate(raw string) []byte { | ||
return formatKey(raw, kCertificatePrefix, kCertificateSuffix, 76) | ||
} | ||
|
||
func ParseCertificate(b []byte) (*x509.Certificate, error) { | ||
block, _ := pem.Decode(b) | ||
if block == nil { | ||
return nil, ErrCertificateFailedToLoad | ||
} | ||
csr, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return csr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package crypto4go | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseCertificate(t *testing.T) { | ||
var k = "MIIE4jCCAsqgAwIBAgIIYsSr5bKAMl8wDQYJKoZIhvcNAQELBQAwejELMAkGA1UEBhMCQ04xFjAUBgNVBAoMDUFudCBGaW5hbmNpYWwxIDAeBgNVBAsMF0NlcnRpZmljYXRpb24gQXV0aG9yaXR5MTEwLwYDVQQDDChBbnQgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5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c/mNhrh+14EQgwuemIIFDLOgcD+iISoN8CqegelNcJndFw1PDN6LkVoiHz9p7jzsge8RKay/QW6C03KNDpWZEUCgCUdfHfo8xKeR+LL1cfn24HKJmZt8L/aeRZwZ1jwePXFRVtiXELvgJuM/tJDIFj2KD337iV64fWcKQ/ydDVGqfDZAdcU4hQdsrPWENwPTQPfVPq2NNLMyIH9+WKx9Ed6/WzeZmIy5ZWpX1TtTolo6OJXQFeItMAjHxW/ZSZTok5IS3FuRhExturaInnzjYpx50a6kS34c5+c8hYq7sAtZ/CNLZmBnBCFDaMQqT8xFZJ5uolUaSeXxg7JFY1QsYp5RKvj4SjFwCGKJ2+hPPe9UyyltxOidNtxjaknOCeBHytOr" | ||
|
||
var cert, err = ParseCertificate(FormatCertificate(k)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Log(cert.Issuer.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package crypto4go | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/sha1" | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"encoding/hex" | ||
) | ||
|
||
func MD5(value []byte) []byte { | ||
var m = md5.New() | ||
m.Write(value) | ||
return m.Sum(nil) | ||
} | ||
|
||
func MD5String(value string) string { | ||
return hex.EncodeToString(MD5([]byte(value))) | ||
} | ||
|
||
func SHA1(value []byte) []byte { | ||
var s = sha1.New() | ||
s.Write(value) | ||
return s.Sum(nil) | ||
} | ||
|
||
func SHA1String(value string) string { | ||
return hex.EncodeToString(SHA1([]byte(value))) | ||
} | ||
|
||
func SHA256(value []byte) []byte { | ||
var s = sha256.New() | ||
s.Write(value) | ||
return s.Sum(nil) | ||
} | ||
|
||
func SHA256String(value string) string { | ||
return hex.EncodeToString(SHA256([]byte(value))) | ||
} | ||
|
||
func SHA512(value []byte) []byte { | ||
var s = sha512.New() | ||
s.Write(value) | ||
return s.Sum(nil) | ||
} | ||
|
||
func SHA512String(value string) string { | ||
return hex.EncodeToString(SHA512([]byte(value))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package crypto4go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/smartwalle/crypto4go | ||
|
||
go 1.12 | ||
|
||
require golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284 h1:rlLehGeYg6jfoyz/eDqDU1iRXLKfR42nnNh57ytKEWo= | ||
golang.org/x/crypto v0.0.0-20190506204251-e1dfcc566284/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package crypto4go | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/md5" | ||
"crypto/sha1" | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"encoding/hex" | ||
) | ||
|
||
func HmacMD5(plaintext, key []byte) []byte { | ||
var h = hmac.New(md5.New, key) | ||
h.Write(plaintext) | ||
return h.Sum(nil) | ||
} | ||
|
||
func HmacMD5String(plaintext, key string) string { | ||
return hex.EncodeToString(HmacMD5([]byte(plaintext), []byte(key))) | ||
} | ||
|
||
func HmacSHA1(plaintext, key []byte) []byte { | ||
var h = hmac.New(sha1.New, key) | ||
h.Write(plaintext) | ||
return h.Sum(nil) | ||
} | ||
|
||
func HmacSHA1String(plaintext, key string) string { | ||
return hex.EncodeToString(HmacSHA1([]byte(plaintext), []byte(key))) | ||
} | ||
|
||
func HmacSHA256(plaintext, key []byte) []byte { | ||
var h = hmac.New(sha256.New, key) | ||
h.Write(plaintext) | ||
return h.Sum(nil) | ||
} | ||
|
||
func HmacSHA256String(plaintext, key string) string { | ||
return hex.EncodeToString(HmacSHA256([]byte(plaintext), []byte(key))) | ||
} | ||
|
||
func HmacSHA512(plaintext, key []byte) []byte { | ||
var h = hmac.New(sha512.New, key) | ||
h.Write(plaintext) | ||
return h.Sum(nil) | ||
} | ||
|
||
func HmacSHA512String(plaintext, key string) string { | ||
return hex.EncodeToString(HmacSHA512([]byte(plaintext), []byte(key))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package crypto4go | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
) | ||
|
||
func formatKey(raw, prefix, suffix string, lineCount int) []byte { | ||
if raw == "" { | ||
return nil | ||
} | ||
raw = strings.Replace(raw, prefix, "", 1) | ||
raw = strings.Replace(raw, suffix, "", 1) | ||
raw = strings.Replace(raw, " ", "", -1) | ||
raw = strings.Replace(raw, "\n", "", -1) | ||
raw = strings.Replace(raw, "\r", "", -1) | ||
raw = strings.Replace(raw, "\t", "", -1) | ||
|
||
var sl = len(raw) | ||
var c = sl / lineCount | ||
if sl%lineCount > 0 { | ||
c = c + 1 | ||
} | ||
|
||
var buf bytes.Buffer | ||
buf.WriteString(prefix + "\n") | ||
for i := 0; i < c; i++ { | ||
var b = i * lineCount | ||
var e = b + lineCount | ||
if e > sl { | ||
buf.WriteString(raw[b:]) | ||
} else { | ||
buf.WriteString(raw[b:e]) | ||
} | ||
buf.WriteString("\n") | ||
} | ||
buf.WriteString(suffix) | ||
return buf.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package crypto4go | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
func ZeroPadding(text []byte, blockSize int) []byte { | ||
var diff = blockSize - len(text)%blockSize | ||
var paddingText = bytes.Repeat([]byte{0}, diff) | ||
return append(text, paddingText...) | ||
} | ||
|
||
func PKCS7Padding(text []byte, blockSize int) []byte { | ||
var diff = blockSize - len(text)%blockSize | ||
var paddingText = bytes.Repeat([]byte{byte(diff)}, diff) | ||
return append(text, paddingText...) | ||
} | ||
|
||
func PKCS7UnPadding(text []byte) []byte { | ||
var length = len(text) | ||
var unpadding = int(text[length-1]) | ||
return text[:(length - unpadding)] | ||
} |
Oops, something went wrong.