random key
Test / testing (1.19.13, ubuntu-latest) (push) Successful in 1m35s Details
Test / testing (>=1.20, ubuntu-latest) (push) Successful in 2m16s Details

This commit is contained in:
Akvicor 2024-02-19 17:54:04 +08:00
parent 9b9a31e4a2
commit 7f855c1202
3 changed files with 106 additions and 2 deletions

View File

@ -28,7 +28,7 @@ import "git.viry.cc/gomod/util"
- [x] Dir Size
- [x] Size Unit, Decimal System and Binary System
- [x] Path Split
- [x] Random String
- [x] Random String, Key
- [x] IP
- [x] Time Calculate
- [x] Tcp Port Checker
@ -263,7 +263,7 @@ Size(1*SizeB + 2*SizeKB).Format(",", true)
- `SplitPath(p string) (head, tail string)` 在第一个`/`处分割,且`/`保留在tail中
- `SplitPathRepeat(p string, repeat int) (head, tail string)` 在第一个`/`处分割,且`/`保留在tail中将tail作为参数再次分割重复repeat次
# Random String
# Random String, Key
返回随机字符串
@ -283,6 +283,15 @@ Size(1*SizeB + 2*SizeKB).Format(",", true)
- `RandomStringAtLeastOnce(length int, str ...string)` 每种类型至少包含一个每个str元素为一个类型默认通过`RandomSlice`生成也可通过传入str来自定义
- `RandomStringWithTimestamp(length int, unix ...int64)` 长度至少为8返回包含时间戳的随机字符串
- `ParseRandomStringWithTimestamp(str string) (int64, string)` 解析包含时间戳的随机字符串
- `RandomKey(l int) *KeyResult` 生成一个长度为l的key的byte数组, 返回包含key的结构体
- `KeyResultFromHexString(hx string) *KeyResult` 解析hex字符串转换为KeyResult
KeyResult下的方法
- `.Bytes` 返回key的byte数组
- `.Upper` 返回hex后的大写字符串长度为Bytes的两倍
- `.Lower` 返回hex后的小写字符串长度为Bytes的两倍
- `.Error`
# IP

View File

@ -2,7 +2,10 @@ package util
import (
"crypto/rand"
"encoding/hex"
"errors"
"math/big"
"strings"
"time"
)
@ -105,3 +108,62 @@ func ParseRandomStringWithTimestamp(str string) (int64, string) {
}
return t, string([]byte(str)[RandomStringWithTimestampTimeLength:])
}
type KeyResult struct {
key []byte
err error
}
func (k *KeyResult) Bytes() []byte {
if k.err != nil {
return []byte{}
}
return k.key
}
func (k *KeyResult) Upper() string {
if k.err != nil {
return ""
}
return strings.ToUpper(hex.EncodeToString(k.key))
}
func (k *KeyResult) Lower() string {
if k.err != nil {
return ""
}
return strings.ToLower(hex.EncodeToString(k.key))
}
func (k *KeyResult) Error() error {
return k.err
}
func NewKeyResult(data []byte, err error) *KeyResult {
res := &KeyResult{
key: make([]byte, len(data)),
err: err,
}
copy(res.key, data)
return res
}
func KeyResultFromHexString(hx string) *KeyResult {
k, err := hex.DecodeString(hx)
if err != nil {
return NewKeyResult(nil, err)
}
return NewKeyResult(k, nil)
}
func RandomKey(l int) *KeyResult {
k := make([]byte, l)
n, err := rand.Read(k)
if err != nil {
return NewKeyResult(nil, err)
}
if n != l {
return NewKeyResult(nil, errors.New("invalid length"))
}
return NewKeyResult(k, nil)
}

View File

@ -1,7 +1,9 @@
package util
import (
"bytes"
"fmt"
"testing"
"time"
)
@ -73,3 +75,34 @@ func ExampleRandomStringWithTimestamp() {
// Output:
//
}
func TestKey(t *testing.T) {
key := RandomKey(17)
if key.Error() != nil {
t.Error(key.Error())
}
if len(key.Bytes()) != 17 {
t.Errorf("expected 17 got %d", len(key.Bytes()))
}
if len(key.Upper()) != 34 {
t.Errorf("expected 34 got %d", len(key.Upper()))
}
dk := KeyResultFromHexString(key.Upper())
if dk.Error() != nil {
t.Error(dk.Error())
}
if len(dk.Upper()) != 34 {
t.Errorf("expected 34 got %d", len(dk.Upper()))
}
if dk.Upper() != key.Upper() {
t.Error("low: dk != key")
}
if dk.Lower() != key.Lower() {
t.Error("up: dk != key")
}
if !bytes.Equal(dk.Bytes(), key.Bytes()) {
t.Error("bytes: dk != key")
}
}