golang-生成csv文件

最近需要使用一组加密数据作为实验数据,方便日后使用就存储到csv文件中

写了个小demo

package main

import (
	"conlock"
	"crypto/ecdsa"
	"crypto/sha256"
	"encoding/csv"
	"encoding/hex"
	"encoding/json"
	"io/ioutil"
	"os"
)

type KeyPool struct {
	Prk *ecdsa.PrivateKey
	Puk ecdsa.PublicKey
}

type KeyPoolByte struct {
	Prk []byte
	Puk []byte
}

func main() {
	phone1 := "18811452903"
	// phone2 := "18251930232"
	// phone3 := "18888812342"
	var kp [1000]KeyPool
	var token = make([]string, 1000)
	var kpb [1000]KeyPoolByte
	for i := 0; i < 1000; i++ {
		kp[i].Prk, kp[i].Puk = conlock.GetKey()
	}
	h := sha256.New()
	h.Write([]byte(phone1))
	hphone := h.Sum(nil)
	file, err := os.OpenFile("initData.csv", os.O_CREATE|os.O_RDWR, 0644)
	os.Create("KeyPool.json")
	for i := 0; i < 1000; i++ {
		os.Chmod("KeyPool.json", 0666)
		kpb[i].Prk, kpb[i].Puk = conlock.ExportECDSAKey(kp[i].Prk, kp[i].Puk)
		jsonkp, err := json.Marshal(kpb)
		if err != nil {
			panic(err)
		}
		err = ioutil.WriteFile("KeyPool.json", jsonkp, os.ModeAppend)
		if err != nil {
			panic(err)
		}
		token[i] = hex.EncodeToString(conlock.Encrypt(hphone, kp[i].Puk))
	}
	file.WriteString("\xEF\xBB\xBF")
	if err != nil {
		panic(err)
	}
	w := csv.NewWriter(file)
	w.Write([]string(token))
	w.Flush()
	defer file.Close()
}

 

你可能感兴趣的:(区块链)