Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Dec 2, 2024
0 parents commit 40bbeff
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* text=auto eol=lf
*.txt eol=lf
*.bat eol=crlf
*.cmd eol=crlf
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files.eol": "\n"
}
8 changes: 8 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/bddjr/cryptorandstr

go 1.21
137 changes: 137 additions & 0 deletions rand.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions rand_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 40bbeff

Please sign in to comment.