An open API service indexing awesome lists of open source software.

https://github.com/leonklingele/randomstring

Cryptographically secure random strings in Go
https://github.com/leonklingele/randomstring

crypto password random random-string secure

Last synced: 9 months ago
JSON representation

Cryptographically secure random strings in Go

Awesome Lists containing this project

README

          

# Cryptographically secure random strings in Go

![test](https://github.com/leonklingele/randomstring/actions/workflows/test.yml/badge.svg)

I was tired of so many Go apps and libraries being [modulo-biased](https://stackoverflow.com/a/10984975) when generating random strings.
Here's another library to generate cryptographically secure and unbiased strings.
Internally this library uses Go's [crypto/rand](https://golang.org/pkg/crypto/rand/) package which reads from the best random generator your system provides.

## tl;dr

```sh
# Install library
go get -u github.com/leonklingele/randomstring/v2
```

```go
// .. and use it. Generate a 30 character long string
s, err := randomstring.Generate(30, randomstring.CharsASCII)
```

```go
// You can even use your own alphabet "abc". Be careful though!
s, err := randomstring.Generate(30, "abc")
```

Supported alphabets:

- `randomstring.CharsNum`: Contains numbers from 0-9
- `randomstring.CharsAlpha`: Contains the full English alphabet: letters a-z and A-Z
- `randomstring.CharsAlphaNum`: Is a combination of CharsNum and CharsAlpha
- `randomstring.CharsASCII`: Contains all printable ASCII characters in code range [32, 126]