github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/crypto_test.go (about)

     1  package f_test
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/hex"
     6  	"github.com/angenalZZZ/gofunc/f"
     7  	"io/ioutil"
     8  	"testing"
     9  )
    10  
    11  func TestMD5(t *testing.T) {
    12  	origData, encryptedString := "hello", "5d41402abc4b2a76b9719d911017c592"
    13  	encryptedStringGo := f.CryptoMD5(origData)
    14  	if encryptedString != encryptedStringGo {
    15  		t.Log(origData)
    16  		t.Log(encryptedStringGo)
    17  		t.Fatal(" encryptedString != encryptedStringGo ")
    18  	}
    19  }
    20  
    21  func TestCryptoAes(t *testing.T) {
    22  	origData := []byte("hello")
    23  	key := []byte("TmIhgugCGFpU7S3v")
    24  	iv := []byte("jkE49230Tf093b42")
    25  	encryptedBytes := f.CryptoAesCBCEncrypt(origData, key, iv)
    26  	encryptedString := hex.EncodeToString(encryptedBytes)
    27  	// Output: CryptoAesCBCEncrypt: hello => 548e8841b4baa92451bc4e7fd875ad1c
    28  	t.Logf("CryptoAesCBCEncrypt: %s => %s", origData, encryptedString)
    29  	encryptedRaw, err3 := hex.DecodeString(encryptedString)
    30  	if err3 != nil {
    31  		t.Fatal(err3)
    32  	}
    33  	if string(encryptedRaw) != string(encryptedBytes) {
    34  		t.Fatal("encryptedRaw != encryptedBytes")
    35  	}
    36  	origDataRaw := f.CryptoAesCBCDecrypt(encryptedRaw, key, iv)
    37  	if string(origDataRaw) != string(origData) {
    38  		t.Fatal("origDataRaw != origData")
    39  	}
    40  	// Output: CryptoAesCBCDecrypt: 548e8841b4baa92451bc4e7fd875ad1c => hello
    41  	t.Logf("CryptoAesCBCDecrypt: %s => %s", hex.EncodeToString(encryptedRaw), origDataRaw)
    42  
    43  	// Crypto: pbkdf2 Rfc2898DeriveBytes password
    44  	salt := []byte("hgt!16kl")
    45  	encrypted1 := f.CryptoAesCBCEncryptWithHmacSHA1(origData, key, salt, iv, 1000, 32)
    46  	encryptedString1 := hex.EncodeToString(encrypted1)
    47  	// Output: CryptoAesCBCEncryptWithHmacSHA1: hello => 7a940d1245ad99fa6cbb4c6fe72f2ed8
    48  	t.Logf("CryptoAesCBCEncryptWithHmacSHA1: %s => %s", origData, encryptedString1)
    49  	encryptedRaw1, err31 := hex.DecodeString(encryptedString1)
    50  	if err31 != nil {
    51  		t.Fatal(err31)
    52  	}
    53  	if string(encryptedRaw1) != string(encrypted1) {
    54  		t.Fatal("encryptedRaw1 != encrypted1")
    55  	}
    56  	origDataRaw1 := f.CryptoAesCBCDecryptWithHmacSHA1(encryptedRaw1, key, salt, iv, 1000, 32)
    57  	if string(origDataRaw1) != string(origData) {
    58  		t.Fatal("origDataRaw1 != origData")
    59  	}
    60  	// Output: CryptoAesCBCDecryptWithHmacSHA1: 7a940d1245ad99fa6cbb4c6fe72f2ed8 => hello
    61  	t.Logf("CryptoAesCBCDecryptWithHmacSHA1: %s => %s", hex.EncodeToString(encryptedRaw1), origDataRaw1)
    62  }
    63  
    64  func TestCryptoDes(t *testing.T) {
    65  	origData := []byte("hello")
    66  	key := []byte("GFpU7S3v")
    67  	iv := []byte("jkE49230Tf093b42")
    68  	encryptedBytes := f.CryptoDesCBCEncrypt(origData, key, iv)
    69  	encryptedString := hex.EncodeToString(encryptedBytes)
    70  	// Output: CryptoDesCBCEncrypt: hello => 898aff98549d75cb
    71  	t.Logf("CryptoDesCBCEncrypt: %s => %s", origData, encryptedString)
    72  	encryptedRaw, err3 := hex.DecodeString(encryptedString)
    73  	if err3 != nil {
    74  		t.Fatal(err3)
    75  	}
    76  	if string(encryptedRaw) != string(encryptedBytes) {
    77  		t.Fatal("encryptedRaw != encryptedBytes")
    78  	}
    79  	origDataRaw := f.CryptoDesCBCDecrypt(encryptedRaw, key, iv)
    80  	if string(origDataRaw) != string(origData) {
    81  		t.Fatal("origDataRaw != origData")
    82  	}
    83  	// Output: CryptoDesCBCDecrypt: 898aff98549d75cb => hello
    84  	t.Logf("CryptoDesCBCDecrypt: %s => %s", hex.EncodeToString(encryptedRaw), origDataRaw)
    85  
    86  	// Crypto: des ECB Triple Encrypt
    87  	key = []byte("TmIhgugCGFpU7S3vGFpU7S3v")
    88  	encrypted := f.CryptoDesECBTripleEncrypt(origData, key)
    89  	encryptedString = hex.EncodeToString(encrypted)
    90  	// Output: CryptoDesECBTripleEncrypt: hello => 86f21066c5ba8c49
    91  	t.Logf("CryptoDesECBTripleEncrypt: %s => %s", origData, encryptedString)
    92  }
    93  
    94  func TestCryptoRSA(t *testing.T) {
    95  	origData := `{"Customer":"gbxy","SecretIdCard":"9c1c0dd59ff33f9ac37bd072ac2df86d","Timestamp":1582777645797}`
    96  	publicKeyPemFile, privateKeyPemFile := "../test/rsa/public.pem", "../test/rsa/private.pem"
    97  	publicKeyPemBytes, err1 := ioutil.ReadFile(publicKeyPemFile)
    98  	if err1 != nil {
    99  		t.Fatal(err1)
   100  	}
   101  	privateKeyPemBytes, err2 := ioutil.ReadFile(privateKeyPemFile)
   102  	if err2 != nil {
   103  		t.Fatal(err2)
   104  	}
   105  	// RSA.Encrypt + base64.Encode
   106  	publicKeyEncrypt := f.NewRSAPublicKey(publicKeyPemBytes)
   107  	encryptedBytes, err1 := publicKeyEncrypt.EncryptPKCS1v15([]byte(origData))
   108  	if err1 != nil {
   109  		t.Fatal(err1)
   110  	}
   111  	encryptedBase64Go := base64.StdEncoding.EncodeToString(encryptedBytes)
   112  	t.Log(origData)
   113  	t.Log(encryptedBase64Go)
   114  	// base64.Decode + RSA.Decrypt
   115  	privateKeyDecrypt := f.NewRSAPrivateKey(privateKeyPemBytes)
   116  	encrypted, _ := base64.StdEncoding.DecodeString(encryptedBase64Go)
   117  	origDataBytes, err2 := privateKeyDecrypt.DecryptPKCS1v15(encrypted)
   118  	if err2 != nil {
   119  		t.Fatal(err2)
   120  	}
   121  	origDataGo := string(origDataBytes)
   122  	if origData != origDataGo {
   123  		t.Log(origDataGo)
   124  		t.Fatal(" origData != origDataGo ")
   125  	}
   126  }
   127  
   128  func TestCryptoAesCBCEncryptWithHmacSHA1_ABP(t *testing.T) {
   129  	key, salt, iv := []byte("TmIhgugCGFpU7S3v"), []byte("hgt!16kl"), []byte("jkE49230Tf093b42")
   130  	origData := []byte(`{"Customer":"gbxy","SecretIdCard":"9c1c0dd59ff33f9ac37bd072ac2df86d","Timestamp":1582777645797}`)
   131  	encryptedBytes := f.CryptoAesCBCEncryptWithHmacSHA1(origData, key, salt, iv, 1000, 32)
   132  	encryptedBase64 := `dpMVVfOuahSQ/e3o9OCKcplkG756+0R7bWMeW931IHHHNbJM9Hif80s80Wt9CMmDK81fN1JpTqiiMmLRtmLo5tzdoGyXIkinSVNokXHNw4HAC5oHljXWs3JKm6W2+D8H`
   133  	encryptedBase64Go := base64.StdEncoding.EncodeToString(encryptedBytes)
   134  	if encryptedBase64 != encryptedBase64Go {
   135  		t.Log(encryptedBase64)
   136  		t.Log(encryptedBase64Go)
   137  		t.Fatal(" encryptedBase64 != encryptedBase64Go ")
   138  	}
   139  	//encryptedBytes = CryptoAesCBCEncryptWithHmacSHA256(origData, key, salt, iv, 1000, 32)
   140  	//encryptedBase64 = "HKSRK014LeYE7xzoPWxkqexBmcvVt3cRqIYhjCIVJ53xbcJ65B3nwaGjoZ7y8+RcOqziIcQhUa0TyoNpG2HHobTF+4bKlNIF1KNO9GtiWJHZe646i0bb3aokH+UPtoqe"
   141  	//encryptedBase64Go = base64.StdEncoding.EncodeToString(encryptedBytes)
   142  	//if encryptedBase64 != encryptedBase64Go {
   143  	//	t.Log(encryptedBase64)
   144  	//	t.Log(encryptedBase64Go)
   145  	//	t.Fatal(" encryptedBase64 != encryptedBase64Go ")
   146  	//}
   147  }