package sm9

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"xdx.jelly/xgcl/sm/sm3"
)

// 生成GBT 38635附录签名示例数据
func TestGBT38635Sign(t *testing.T) {

	uid := []byte("Alice")
	msg := []byte("Chinese IBS standard")
	digest := sm3.Sum(msg)
	msg = digest[:]
	fmt.Printf("待签名消息的杂凑M: %s\n", strings.ToUpper(hex.EncodeToString(msg)))

	rnd, _ := hex.DecodeString("000130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4")
	ks, pubs, _ := GenerateMastSignPrivateKey(bytes.NewReader(rnd))
	ds, err := ks.GenerateUserSignKey(uid)
	assert.Nil(t, err)

	// 打印
	fmt.Printf("签名主私钥ks:%s\n", strings.ToUpper(ks.String()))
	fmt.Printf("签名主公钥Pubs:%s\n", strings.ToUpper(pubs.G2.String()))

	fmt.Printf("实体A的标识:%s\n", string(uid))
	fmt.Printf("实体A的签名私钥ds:%s\n", ds.G1.String())

	if PrintIntermediaDataForSTD38635 {
		fmt.Printf("\n================= 签名步骤中的相关值 ===================\n")
	}
	rnd, _ = hex.DecodeString("00033C8616B06704813203DFD00965022ED15975C662337AED648835DC4B1CBE")
	signature, _ := Sign(msg, ds, pubs, rnd)

	if PrintIntermediaDataForSTD38635 {
		fmt.Printf("\n================= 验证步骤中的相关值 ===================\n")
	}
	if !Verify(signature, uid, msg, pubs) {
		t.Fatal()
	}
}
