框架基本搭建完成

This commit is contained in:
2022-04-03 12:30:50 +08:00
commit ec4c957b25
31 changed files with 1353 additions and 0 deletions

8
pkg/utils/file.go Normal file
View File

@@ -0,0 +1,8 @@
package utils
import "os"
func FileExist(path string) bool {
_, err := os.Lstat(path)
return os.IsExist(err)
}

84
pkg/utils/key.go Normal file
View File

@@ -0,0 +1,84 @@
package utils
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
)
func DecodePublicKey(encodedKey []byte) (*ecdsa.PublicKey, error) {
block, _ := pem.Decode(encodedKey)
if block == nil || block.Type != "PUBLIC KEY" {
return nil, fmt.Errorf("marshal: could not decode PEM block type %s", block.Type)
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
ecdsaPub, ok := pub.(*ecdsa.PublicKey)
if !ok {
return nil, errors.New("marshal: data was not an ECDSA public key")
}
return ecdsaPub, nil
}
func EncodePublicKey(key *ecdsa.PublicKey) ([]byte, error) {
derBytes, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return nil, err
}
block := &pem.Block{
Type: "PUBLIC KEY",
Bytes: derBytes,
}
return pem.EncodeToMemory(block), nil
}
func DecodePrivateKey(encodedKey []byte) (*ecdsa.PrivateKey, error) {
var skippedTypes []string
var block *pem.Block
for {
block, encodedKey = pem.Decode(encodedKey)
if block == nil {
return nil, fmt.Errorf("failed to find EC PRIVATE KEY in PEM data after skipping types %v", skippedTypes)
}
if block.Type == "EC PRIVATE KEY" {
break
} else {
skippedTypes = append(skippedTypes, block.Type)
continue
}
}
privKey, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privKey, nil
}
func EncodePrivateKey(key *ecdsa.PrivateKey) ([]byte, error) {
derKey, err := x509.MarshalECPrivateKey(key)
if err != nil {
return nil, err
}
keyBlock := &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: derKey,
}
return pem.EncodeToMemory(keyBlock), nil
}

32
pkg/utils/key_test.go Normal file
View File

@@ -0,0 +1,32 @@
package utils
import (
"io/ioutil"
"testing"
)
func TestDecodePrivateKey(t *testing.T) {
priKeyStr, err := ioutil.ReadFile("../../config/id_ecdsa")
if err != nil {
t.Fatal(err)
}
key, err := DecodePrivateKey(priKeyStr)
if err != nil {
t.Error(err)
}
t.Log(key)
}
func TestDecodePublicKey(t *testing.T) {
priKeyStr, err := ioutil.ReadFile("../../config/id_ecdsa.pub")
if err != nil {
t.Fatal(err)
}
key, err := DecodePublicKey(priKeyStr)
if err != nil {
t.Error(err)
}
t.Log(key)
}

21
pkg/utils/password.go Normal file
View File

@@ -0,0 +1,21 @@
package utils
import (
"github.com/alexedwards/argon2id"
)
func GenPasswd(originPasswd string) (passwd string, err error) {
passwd, err = argon2id.CreateHash(originPasswd, argon2id.DefaultParams)
if err != nil {
return "", err
}
return passwd, nil
}
func CheckPasswd(originPasswd string, passwd string) (match bool, err error) {
match, err = argon2id.ComparePasswordAndHash(originPasswd, passwd)
if err != nil {
return false, err
}
return match, nil
}

View File

@@ -0,0 +1,41 @@
package utils
import (
"testing"
)
//hashed := fmt.Sprintf("%x", sha256.Sum256([]byte("pa$$word"+"drivelinked")))
var hashed = "2bd6e406749d3fb8ff5c164ee63c4fdc744164f7327ea3ad6abef90df4e64d03"
func TestGenPasswd(t *testing.T) {
passwd, err := GenPasswd(hashed)
if err != nil {
t.Error(err)
}
t.Log(passwd)
}
func TestCheckPasswd(t *testing.T) {
passwd, err := GenPasswd(hashed)
if err != nil {
t.Error(err)
}
// 匹配
match, err := CheckPasswd(hashed, passwd)
if err != nil {
t.Error(err)
}
if !match {
t.Fail()
}
// 不匹配
match, err = CheckPasswd("abc", passwd)
if err != nil {
t.Error(err)
}
if match {
t.Fail()
}
}

25
pkg/utils/snowflake.go Normal file
View File

@@ -0,0 +1,25 @@
package utils
import "github.com/bwmarrin/snowflake"
var SfNode *snowflake.Node
func init() {
var (
err error
)
// 初始化
SfNode, err = snowflake.NewNode(1)
if err != nil {
return
}
}
func Snowflake() snowflake.ID {
return SfNode.Generate()
}
func SnowflakeInt64() int64 {
return Snowflake().Int64()
}

View File

@@ -0,0 +1,13 @@
package utils
import "testing"
func TestSnowflake(t *testing.T) {
id := Snowflake()
t.Log(id.String())
}
func TestSnowflakeInt64(t *testing.T) {
id := SnowflakeInt64()
t.Log(id)
}