From 40bbeff4727550c8bfd5de7c6a7015d7116568fe Mon Sep 17 00:00:00 2001 From: bddjr Date: Mon, 2 Dec 2024 16:22:55 +0800 Subject: [PATCH] first commit --- .gitattributes | 4 ++ .vscode/settings.json | 3 + LICENSE.txt | 8 +++ README.md | 48 +++++++++++++++ go.mod | 3 + rand.go | 137 ++++++++++++++++++++++++++++++++++++++++++ rand_test.go | 44 ++++++++++++++ 7 files changed, 247 insertions(+) create mode 100644 .gitattributes create mode 100644 .vscode/settings.json create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 go.mod create mode 100644 rand.go create mode 100644 rand_test.go diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6621d77 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +* text=auto eol=lf +*.txt eol=lf +*.bat eol=crlf +*.cmd eol=crlf diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..37441be --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "files.eol": "\n" +} \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..8e5d5a3 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright © 2024 bddjr + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1b61f52 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# crypto random string + +Quickly generate secure random string. + +## Get + +``` +go get github.com/bddjr/cryptorandstr +``` + +## Test + +``` +MustRand10 6 + 961812 + 186418 + 476781 + 683053 + 189178 + +MustRand16 27 + d4ff1e125208f0e2954001b3c6e + 36c517865848a7444454a7711ca + f5348bba35053bfeb962d3822b9 + df46ffa337732945d6c98ed0276 + 280a987f779f9775d8decffb150 + +MustRand32 27 + fjq0iakoubcl2sp92ekaq9ftsan + mgu2bms5d90ai35mtjdrru3rkfe + pt6k8rmsneovn3bq3q1ru2m26bk + 91rn0793voe4tupdcm2b31rfdd1 + ank42vo7ju49t0irpmmn65osj5p + +MustRand64 27 + bMPa2jTkoJF4NzWgv4wr6Qmc-OK + _oWdmJSErNbW1rval-gM7ult-6_ + KhMddFuOkKCf9eHXx3s11H4AZmE + oCvBxMPnZwLV_Bi540RnH8_Nmk6 + st-G5VLmETyHV8WLhAOEaIWfuEn + +PASS +ok github.com/bddjr/cryptorandstr 0.253s +``` + +## License + +[MIT](LICENSE.txt) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3758206 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/bddjr/cryptorandstr + +go 1.21 diff --git a/rand.go b/rand.go new file mode 100644 index 0000000..b6cd1c4 --- /dev/null +++ b/rand.go @@ -0,0 +1,137 @@ +package cryptorandstr + +import ( + "crypto/rand" + "fmt" +) + +const Chars64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_" + +var Chars10 = Chars64[:10] +var Chars16 = Chars64[:16] +var Chars32 = Chars64[:32] + +// Panic when error. +func MustRand10(strLen int) string { + return MustRand(strLen, Chars10) +} + +// Panic when error. +func MustRand16(strLen int) string { + return MustRand(strLen, Chars16) +} + +// Panic when error. +func MustRand32(strLen int) string { + return MustRand(strLen, Chars32) +} + +// Panic when error. +func MustRand64(strLen int) string { + return MustRand(strLen, Chars64) +} + +// panic when error. +func MustRand(strLen int, chars string) string { + s, err := Rand(strLen, chars) + if err != nil { + panic(err) + } + return s +} + +func Rand10(strLen int) (string, error) { + return Rand(strLen, Chars10) +} + +func Rand16(strLen int) (string, error) { + return Rand(strLen, Chars16) +} + +func Rand32(strLen int) (string, error) { + return Rand(strLen, Chars32) +} + +func Rand64(strLen int) (string, error) { + return Rand(strLen, Chars64) +} + +func Rand(strLen int, chars string) (string, error) { + if strLen < 1 { + return "", fmt.Errorf("cryptorandstr: strLen %d < 1", strLen) + } + if len(chars) < 2 { + return "", fmt.Errorf("cryptorandstr: chars length %d < 2", len(chars)) + } + if len(chars) > 256 { + return "", fmt.Errorf("cryptorandstr: chars length %d > 256", len(chars)) + } + + chunk := make([]byte, 1) + maxB := byte(len(chars) - 1) + r := randWithGC{ + outBitLen: bitLen(maxB), + } + + out := make([]byte, strLen) + i := 0 + for i < strLen { + b, err := r.byte(chunk) + if err != nil { + return "", err + } + if b <= maxB { + out[i] = chars[b] + i++ + } + } + + return string(out), nil +} + +func bitLen(b byte) (l byte) { + for b != 0 { + b >>= 1 + l++ + } + return +} + +type randWithGC struct { + outBitLen byte + cacheBitLen byte + cache uint16 +} + +// len(chunk) == 1 +func (r *randWithGC) byte(chunk []byte) (byte, error) { + difference := 8 - r.outBitLen + + // read cache + if r.cacheBitLen >= r.outBitLen { + b := (byte(r.cache) << difference) >> difference + r.cache >>= r.outBitLen + r.cacheBitLen -= r.outBitLen + return b, nil + } + + // random + for { + n, err := rand.Reader.Read(chunk) + if err != nil { + return 0, err + } + if n == 1 { + break + } + } + + b := chunk[0] + + // write cache + r.cache <<= difference + r.cache |= uint16(b >> r.outBitLen) + r.cacheBitLen += difference + + return (b << difference) >> difference, nil +} diff --git a/rand_test.go b/rand_test.go new file mode 100644 index 0000000..4c0012d --- /dev/null +++ b/rand_test.go @@ -0,0 +1,44 @@ +package cryptorandstr_test + +import ( + "fmt" + "testing" + + "github.com/bddjr/cryptorandstr" +) + +const strLen = 27 +const n = 5 + +func TestMustRand10(t *testing.T) { + const strLen = 6 + fmt.Println("MustRand10", strLen) + for i := 0; i < n; i++ { + fmt.Println(" ", cryptorandstr.MustRand10(strLen)) + } + fmt.Println() +} + +func TestMustRand16(t *testing.T) { + fmt.Println("MustRand16", strLen) + for i := 0; i < n; i++ { + fmt.Println(" ", cryptorandstr.MustRand16(strLen)) + } + fmt.Println() +} + +func TestMustRand32(t *testing.T) { + fmt.Println("MustRand32", strLen) + for i := 0; i < n; i++ { + fmt.Println(" ", cryptorandstr.MustRand32(strLen)) + } + fmt.Println() +} + +func TestMustRand64(t *testing.T) { + fmt.Println("MustRand64", strLen) + for i := 0; i < n; i++ { + fmt.Println(" ", cryptorandstr.MustRand64(strLen)) + } + fmt.Println() +}