random key
This commit is contained in:
parent
9b9a31e4a2
commit
7f855c1202
13
README.md
13
README.md
|
@ -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
|
||||
|
||||
|
|
62
random.go
62
random.go
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue