package sm

import (
	"PPOSUtils/sm/sm2"
	"PPOSUtils/sm/sm4"
	"PPOSUtils/utils"
	"encoding/base64"
	"encoding/hex"
	"testing"
)

//SM4-Ecb-加密
func TestSM4Ecb_Do(t *testing.T) {
	_in := []byte("你好")
	_key := []byte("papa20140924papa")
	_plainTextWithPadding := utils.PKCS5Padding(_in, sm4.BlockSize)
	_cipherText, _err := sm4.ECBEncrypt(_key, _plainTextWithPadding)
	if _err != nil {
		t.Error(_err)
	}
	_encryptData := base64.StdEncoding.EncodeToString(_cipherText)
	t.Log(_encryptData)
}

//SM4-Ecb-解密
func TestSM4Dec_Do(t *testing.T) {
	_decodeStr := "wYzN6/xK821Rg9hTOVcFSQ=="
	_key := []byte("papa20140924papa")
	_base64Str, _err := base64.StdEncoding.DecodeString(_decodeStr)
	if _err != nil {
		t.Error(_err)
	}
	//
	_plainTextWithPadding, _err := sm4.ECBDecrypt(_key, _base64Str)
	if _err != nil {
		t.Error(_err)
	}
	_plainText := utils.PKCS5UnPadding(_plainTextWithPadding)
	t.Log(string(_plainText))
}

//SM2公钥加密
func TestSM2Ecb_Do(t *testing.T) {
	_pubKey := ""
	_sm4Key := []byte("papa20140924papa")
	_hex, _ := hex.DecodeString(_pubKey)
	if len(_hex) > 64 {
		_hex = _hex[len(_hex)-64:]
	}
	_pub, _err := sm2.RawBytesToPublicKey(_hex)
	if _err != nil {
		t.Error("RawBytesToPublicKey:" + _err.Error())
	}
	_data := base64.StdEncoding.EncodeToString([]byte(_sm4Key))
	cipherText, _err := sm2.Encrypt(_pub, []byte(_data), sm2.C1C2C3)
	if _err != nil {
		t.Error(_err)
	}
	_cipherTxt := hex.EncodeToString(cipherText)
	t.Log(_cipherTxt)
}
