Skip to content

Commit 51176de

Browse files
committed
feat: add README
1 parent ac14ea8 commit 51176de

File tree

1 file changed

+234
-0
lines changed

1 file changed

+234
-0
lines changed

README.md

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
# Crypto Random String
2+
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/brijeshshah13/crypto-random-string.svg)](https://pkg.go.dev/github.com/brijeshshah13/crypto-random-string)
4+
5+
You can use this library to generate a cryptographically strong random string which can be useful for creating an
6+
identifier, slug, salt, PIN code, fixture, etc.
7+
8+
## Installation
9+
10+
```shell
11+
go get -u github.com/brijeshshah13/crypto-random-string
12+
```
13+
14+
## Usage
15+
16+
### Using only `length`
17+
18+
```golang
19+
package main
20+
21+
import (
22+
"fmt"
23+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
24+
)
25+
26+
func main() {
27+
generator := cryptorandomstring.New()
28+
if str, err := generator.WithLength(10).Generate(); err != nil {
29+
panic(err)
30+
} else {
31+
fmt.Println(str) // => "c152f80d02"
32+
}
33+
}
34+
```
35+
36+
### Using `kind: "base64"`
37+
38+
```golang
39+
package main
40+
41+
import (
42+
"fmt"
43+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
44+
)
45+
46+
func main() {
47+
generator := cryptorandomstring.New()
48+
if str, err := generator.WithLength(10).WithKind("base64").Generate(); err != nil {
49+
panic(err)
50+
} else {
51+
fmt.Println(str) // => "e3WdumTFMK"
52+
}
53+
}
54+
```
55+
56+
### Using `kind: "url-safe"`
57+
58+
```golang
59+
package main
60+
61+
import (
62+
"fmt"
63+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
64+
)
65+
66+
func main() {
67+
generator := cryptorandomstring.New()
68+
if str, err := generator.WithLength(10).WithKind("url-safe").Generate(); err != nil {
69+
panic(err)
70+
} else {
71+
fmt.Println(str) // => "A5WT-V~iG4"
72+
}
73+
}
74+
```
75+
76+
### Using `kind: "numeric"`
77+
78+
```golang
79+
package main
80+
81+
import (
82+
"fmt"
83+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
84+
)
85+
86+
func main() {
87+
generator := cryptorandomstring.New()
88+
if str, err := generator.WithLength(10).WithKind("numeric").Generate(); err != nil {
89+
panic(err)
90+
} else {
91+
fmt.Println(str) // => "7917906408"
92+
}
93+
}
94+
```
95+
96+
### Using `kind: "distinguishable"`
97+
98+
```golang
99+
package main
100+
101+
import (
102+
"fmt"
103+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
104+
)
105+
106+
func main() {
107+
generator := cryptorandomstring.New()
108+
if str, err := generator.WithLength(10).WithKind("distinguishable").Generate(); err != nil {
109+
panic(err)
110+
} else {
111+
fmt.Println(str) // => "0WT1KUR4ER"
112+
}
113+
}
114+
```
115+
116+
### Using `kind: "ascii-printable"`
117+
118+
```golang
119+
package main
120+
121+
import (
122+
"fmt"
123+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
124+
)
125+
126+
func main() {
127+
generator := cryptorandomstring.New()
128+
if str, err := generator.WithLength(10).WithKind("ascii-printable").Generate(); err != nil {
129+
panic(err)
130+
} else {
131+
fmt.Println(str) // => "b|QK|LS"LN"
132+
}
133+
}
134+
```
135+
136+
### Using `kind: "alphanumeric"`
137+
138+
```golang
139+
package main
140+
141+
import (
142+
"fmt"
143+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
144+
)
145+
146+
func main() {
147+
generator := cryptorandomstring.New()
148+
if str, err := generator.WithLength(10).WithKind("alphanumeric").Generate(); err != nil {
149+
panic(err)
150+
} else {
151+
fmt.Println(str) // => "WyK7545i98"
152+
}
153+
}
154+
```
155+
156+
### Using `characters`
157+
158+
```golang
159+
package main
160+
161+
import (
162+
"fmt"
163+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
164+
)
165+
166+
func main() {
167+
generator := cryptorandomstring.New()
168+
if str, err := generator.WithLength(10).WithCharacters("abc").Generate(); err != nil {
169+
panic(err)
170+
} else {
171+
fmt.Println(str) // => "abbbccbaab"
172+
}
173+
}
174+
```
175+
176+
### Default Scraper (Ad hoc)
177+
178+
In simple cases, you can use the default scraper without creating an object instance
179+
180+
```golang
181+
package main
182+
183+
import (
184+
"fmt"
185+
cryptorandomstring "github.com/brijeshshah13/crypto-random-string"
186+
)
187+
188+
func main() {
189+
if str, err := cryptorandomstring.WithLength(10).WithCharacters("abc").Generate(); err != nil {
190+
panic(err)
191+
} else {
192+
fmt.Println(str)
193+
}
194+
}
195+
```
196+
197+
## API Options
198+
199+
### `length` with `WithLength`
200+
201+
*Required*\
202+
Type: `uint64`
203+
204+
### `kind` with `WithKind`
205+
206+
Type: `string`\
207+
Default: `'hex'`\
208+
Values: `'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable' | 'alphanumeric'`
209+
210+
Use only characters from a predefined set of allowed characters.
211+
212+
Cannot be set at the same time as the `characters` option.
213+
214+
The `distinguishable` set contains only uppercase characters that are not easily confused: `CDEHKMPRTUWXY012458`. It can
215+
be useful if you need to print out a short string that you'd like users to read and type back in with minimal errors.
216+
For example, reading a code off of a screen that needs to be typed into a phone to connect two devices.
217+
218+
The `ascii-printable` set contains
219+
all [printable ASCII characters](https://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters): ``!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~``
220+
Useful for generating passwords where all possible ASCII characters should be used.
221+
222+
The `alphanumeric` set contains uppercase letters, lowercase letters, and
223+
digits: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`. Useful for
224+
generating [nonce](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/nonce) values.
225+
226+
### `characters` with `WithCharacters`
227+
228+
Type: `string`\
229+
Minimum length: `1`\
230+
Maximum length: `65536`
231+
232+
Use only characters from a custom set of allowed characters.
233+
234+
Cannot be set at the same time as the `type` option.

0 commit comments

Comments
 (0)