gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/txsec/xt_sec_factory_test.go (about)

     1  package txsec
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  func TestXTSecALL(t *testing.T) {
    10  
    11  	sec := GetDefaultXTSex()
    12  	//testHexP10 := "3081CF307C020102301C310B300906035504060C02434E310D300B06035504030C04C0EEB3E53059301306072A8648CE3D020106082A811CCF5501822D03420004E1D9341D816DEE8B5983F8D9A270E26D3AF39BA411DA21730813C69FEC83E9B3E8C0D76A2C7E0F46C0F3E738D8F711D4DEF6A8A2445FB2E8828F97DBCB0DFE13300C06082A811CCF55018375050003410076C0A6D39D11D1C5358704E7A2033FE342B4DFB0BF80A67C41FB87F99DED46A10F20AAE141502C36121D192D58FA9010FF20FB9E5BC2AA838AFDE7CD4119CE71"
    13  	//testP10, _ := hex.DecodeString(testHexP10)
    14  	//testBase64P10 := base64.StdEncoding.EncodeToString(testP10)
    15  	//fmt.Println("testBase64P10 : ", testBase64P10)
    16  	//
    17  	sec.GenKeyPair("302308301941")
    18  	p10, _ := sec.MakeP10("302308301941", "C=CN,CN=张三")
    19  	p10String := base64.StdEncoding.EncodeToString(p10)
    20  	fmt.Println("p10String : ", p10String)
    21  
    22  	cert := "MIICIDCCAcWgAwIBAgIOAQi9JI+jI7DQ+6dMiDUwDAYIKoEcz1UBg3UFADAyMRQwEgYDVQQDDAtTTTIgUk9PVCBDQTENMAsGA1UECgwEVEVTVDELMAkGA1UEBhMCQ04wHhcNMjMwOTA2MTQyNTM2WhcNMjMxMDA2MTQyNTM2WjAhMQswCQYDVQQGDAJDTjESMBAGA1UEAwwJ5a+u54qx56yBMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEWAypb1xoJECYdLXJbWhXAjlt9TKMClyZEb2DjKm1ayKX4i/CJv8zjmqEGJFfxzD3296SyCxTFVyfXU5h9qzVVaOBzTCByjAMBgNVHRMBAf8EAjAAMB8GA1UdIwQYMBaAFJBE0BKTH+FuyO30U1ZrCrX2ZhaeMB0GA1UdDgQWBBSK/dQatlJeErlomWPF2wOeoz+bWjALBgNVHQ8EBAMCB4AwbQYDVR0fBGYwZDBioCOgIYYfaHR0cDovLzEyNC4xMjcuNDkuMTgxL2NybC8zLmNybIEDB0KAojakNDAyMRQwEgYDVQQDDAtTTTIgUk9PVCBDQTENMAsGA1UECgwEVEVTVDELMAkGA1UEBhMCQ04wDAYIKoEcz1UBg3UFAANHADBEAiAA+QKH3kv/B5Az1a8v/iz4VABnosP3ZSV0ojB8EKlcUwIgRE0mTt/KxWyiScSNWW+GxyP6F2D4DI/b0FVpo4bUqjA="
    23  	signCertByte, _ := base64.StdEncoding.DecodeString(cert)
    24  
    25  	fmt.Println("signCertByte.len : ", len(signCertByte))
    26  	sec.SaveCert("302308301941", signCertByte, len(signCertByte))
    27  
    28  	pubKey, _ := sec.GetPubKey("302308301941")
    29  
    30  	signData, _ := sec.Sign("302308301941", []byte("测试数据"))
    31  	fmt.Println("signData : ", base64.StdEncoding.EncodeToString(signData))
    32  
    33  	verify := sec.VerifySign(pubKey, []byte("测试数据"), signData)
    34  	fmt.Println("verify : ", verify)
    35  
    36  	encData, _ := sec.Encrypt(pubKey, []byte("123"))
    37  	fmt.Println("encData : ", base64.StdEncoding.EncodeToString(encData))
    38  
    39  	msg, _ := sec.Decrypt("302308301941", encData)
    40  	fmt.Println("msg : ", string(msg))
    41  
    42  	sec.ChangeContId("302308301941", "302308301942")
    43  
    44  }
    45  
    46  //func main() {
    47  //
    48  //	lib, err := dl.Open("/opt/XTSec/lib/libXTSec.so", 0)
    49  //	if err != nil {
    50  //		panic(err)
    51  //	}
    52  //	defer lib.Close()
    53  //
    54  //	userLable := C.CString("302308291606")
    55  //	defer C.free(unsafe.Pointer(userLable))
    56  //
    57  //	//var XT_GenKeyPair func(szContId *C.char, pucPub *C.uchar, puiPubLen *C.uint) *C.int
    58  //	//lib.Sym("XT_GenKeyPair", &XT_GenKeyPair)
    59  //	//
    60  //	//cPublicKey := [256]byte{}
    61  //	//
    62  //	//var cPublicKeyTemp *C.uchar = (*C.uchar)(unsafe.Pointer(&cPublicKey[0]))
    63  //	//var puiPubLen *C.uint
    64  //	//
    65  //	//ret := XT_GenKeyPair(userLable, cPublicKeyTemp, puiPubLen)
    66  //	//fmt.Println("XT_GenKeyPair:", ret)
    67  //	//fmt.Println("XT_GenKeyPair:", puiPubLen)
    68  //	//cPublicKey := C.GoBytes(unsafe.Pointer(cPublicKeyTemp))
    69  //	//
    70  //	//fmt.Println("XT_GenKeyPair:", hex.EncodeToString(cPublicKey))
    71  //
    72  //	cPublicKeyTemp := [256]byte{}
    73  //	puiPubLen := 0
    74  //	var XT_GenKeyPair func(szContId uintptr, pucPub uintptr, puiPubLen uintptr) uintptr
    75  //	lib.Sym("XT_GenKeyPair", &XT_GenKeyPair)
    76  //	ret := XT_GenKeyPair(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&cPublicKeyTemp[0])), uintptr(unsafe.Pointer(&puiPubLen)))
    77  //	fmt.Println("XT_GenKeyPair:", ret)
    78  //
    79  //	// 公钥
    80  //	cPublicKey := []byte{}
    81  //	for i := 0; i < puiPubLen; i++ {
    82  //		cPublicKey = append(cPublicKey, cPublicKeyTemp[i])
    83  //	}
    84  //	fmt.Println("XtGetpubkey cPublicKey:", hex.EncodeToString(cPublicKey))
    85  //
    86  //	//typedef int(WINAPI *PFN_XT_GetPubKey)(IN const char *szContId, IN int iType, OUT unsigned char *pucPub, OUT unsigned int *puiPubLen);
    87  //	var getPubKey func(szContId uintptr, iType uintptr, pucPub uintptr, puiPubLen uintptr) uintptr
    88  //	lib.Sym("XT_GetPubKey", &getPubKey)
    89  //	ret = getPubKey(uintptr(unsafe.Pointer(userLable)), uintptr(0), uintptr(unsafe.Pointer(&cPublicKeyTemp[0])), uintptr(unsafe.Pointer(&puiPubLen)))
    90  //	fmt.Println("XtGetpubkey ret:", ret)
    91  //	//fmt.Println("XtGetpubkey puiPubLen:", puiPubLen)
    92  //	// 公钥
    93  //	cPublicKey = []byte{}
    94  //	for i := 0; i < puiPubLen; i++ {
    95  //		cPublicKey = append(cPublicKey, cPublicKeyTemp[i])
    96  //	}
    97  //	fmt.Println("XtGetpubkey cPublicKey:", hex.EncodeToString(cPublicKey))
    98  //
    99  //	signDataTemp := [256]byte{}
   100  //	signDataLen := 0
   101  //
   102  //	msg := []byte("测试数据")
   103  //
   104  //	//typedef int(WINAPI *PFN_XT_Sign)(IN const char *szContId, IN unsigned char *pucIn, IN unsigned int uiInLen, OUT unsigned char *pucSign, OUT unsigned int *puiSignLen);
   105  //	var sign func(szContId uintptr, pucIn uintptr, uiInLen uintptr, pucSign uintptr, puiSignLen uintptr) uintptr
   106  //	lib.Sym("XT_Sign", &sign)
   107  //	ret = sign(uintptr(unsafe.Pointer(userLable)), uintptr(unsafe.Pointer(&msg[0])), uintptr(len(msg)),
   108  //		uintptr(unsafe.Pointer(&signDataTemp[0])), uintptr(unsafe.Pointer(&signDataLen)))
   109  //
   110  //	fmt.Println("XtSign ret:", ret)
   111  //	fmt.Println("XtSign signDataLen:", signDataLen)
   112  //
   113  //	// 签名值
   114  //	signData := []byte{}
   115  //	for i := 0; i < signDataLen; i++ {
   116  //		signData = append(signData, signDataTemp[i])
   117  //	}
   118  //	fmt.Println("XtSign signData:", hex.EncodeToString(signData))
   119  //
   120  //	pucPub := cPublicKey
   121  //
   122  //	//typedef int(WINAPI *PFN_XT_VerifySign)(IN unsigned char *pucPub, IN unsigned int uiPubLen, IN unsigned char *pucIn, IN unsigned int uiInLen, IN unsigned char *pucSign, IN unsigned int uiSignLen);
   123  //	var verifySign func(pucPub uintptr, uiPubLen uintptr, pucIn uintptr, uiInLen uintptr, pucSign uintptr, uiSignLen uintptr) uintptr
   124  //	lib.Sym("XT_VerifySign", &verifySign)
   125  //	ret = verifySign(uintptr(unsafe.Pointer(&pucPub[0])), uintptr(len(pucPub)), uintptr(unsafe.Pointer(&msg[0])), uintptr(len(msg)), uintptr(unsafe.Pointer(&signData[0])), uintptr(len(signData)))
   126  //	fmt.Println("XtVerifysign ret:", ret)
   127  //
   128  //}