sample_chain/common/kgc/util.go

176 lines
3.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package kgc
import (
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
pb "schain/proto"
"schain/proto/util"
)
func readTxtFile(filePath string) ([]byte, error) {
// 打开文件
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
// 读取文件内容
content, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return content, nil
}
func LoadPriKey(file string) []byte {
content, err := readTxtFile(file)
if err != nil {
fmt.Println("load privatekey error")
}
return content
}
func LoadPubKey(file string) []byte {
content, err := readTxtFile(file)
if err != nil {
fmt.Println("load publickey error")
}
return content
}
func InitIdentity(IdentityPath string, mspID string) ([]byte, error) {
pubkey, _ := readTxtFile(IdentityPath)
sId := &pb.Creator{
Mspid: mspID,
IdBytes: pubkey,
}
return util.MarshalOrPanic(sId), nil
}
func string_halve(str string) (string, string) {
// 计算字符串长度
length := len(str)
// 将字符串分成两部分
mid := length / 2
part1 := str[:mid]
part2 := str[mid:]
//fmt.Println("第一部分:", part1)
//fmt.Println("第二部分:", part2)
return part1, part2
}
func string_trisect(str string) (string, string, string) {
length := len(str)
// 计算每个子字符串的长度
subStrLen := length / 3
// 切分字符串
part1 := str[:subStrLen]
part2 := str[subStrLen : 2*subStrLen]
part3 := str[2*subStrLen:]
return part1, part2, part3
}
func bytes_halve(slice []byte) ([]byte, []byte) {
length := len(slice)
// 计算每个部分的长度
halfLength := length / 2
// 切分切片
part1 := slice[:halfLength]
part2 := slice[halfLength:]
return part1, part2
}
func bytes_trisect(slice []byte) ([]byte, []byte, []byte) {
length := len(slice)
// 计算每个部分的长度
partLength := length / 3
// 切分切片
part1 := slice[:partLength]
part2 := slice[partLength : 2*partLength]
part3 := slice[2*partLength:]
return part1, part2, part3
}
func mergeBigInts(bigInts ...*big.Int) []byte {
var mergedBytes []byte
for _, num := range bigInts {
buf := make([]byte, 32)
num.FillBytes(buf)
//fmt.Println("各个部分长度为:", len(numBytes))
mergedBytes = append(mergedBytes, buf...)
}
return mergedBytes
}
func readFileFromDirectory(directory, filename string) ([]byte, error) {
// 构造文件的完整路径
filePath := filepath.Join(directory, filename)
// 读取文件内容
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
return data, nil
}
func writeToFolder(directory, filename string, data []byte) error {
// 检查 cert 文件夹是否存在
if _, err := os.Stat(directory); os.IsNotExist(err) {
// 如果文件夹不存在,则创建它
err := os.Mkdir(directory, 0755)
if err != nil {
return fmt.Errorf("创建文件夹时出错: %v", err)
}
}
// 构造文件的完整路径
filePath := filepath.Join(directory, filename)
err := os.WriteFile(filePath, []byte(data), 0644)
if err != nil {
return fmt.Errorf("写入文件时出错: %v", err)
}
fmt.Println("字符串已成功写入文件:", filePath)
return nil
}
func BytesToBig(bytes []byte) *big.Int {
//不能直接转为big转为字符串后用16进制解码
big := new(big.Int)
big.SetString(string(bytes), 16)
return big
}
func AnalSig(sig []byte) (*big.Int, *big.Int, *big.Int) {
uxbytes, uybytes, vbytes := bytes_trisect(sig)
//fmt.Println("打印长度:", len(uxbytes))
//fmt.Println(len(uybytes))
Ux := new(big.Int).SetBytes(uxbytes)
Uy := new(big.Int).SetBytes(uybytes)
v := new(big.Int).SetBytes(vbytes)
return Ux, Uy, v
}