.
This commit is contained in:
1
vendor/github.com/matoous/go-nanoid/v2/.gitignore
generated
vendored
Normal file
1
vendor/github.com/matoous/go-nanoid/v2/.gitignore
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.idea
|
||||
21
vendor/github.com/matoous/go-nanoid/v2/LICENSE
generated
vendored
Normal file
21
vendor/github.com/matoous/go-nanoid/v2/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 Matous Dzivjak <matousdzivjak@gmail.com>
|
||||
|
||||
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.
|
||||
21
vendor/github.com/matoous/go-nanoid/v2/Makefile
generated
vendored
Normal file
21
vendor/github.com/matoous/go-nanoid/v2/Makefile
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
# Make this makefile self-documented with target `help`
|
||||
.PHONY: help
|
||||
.DEFAULT_GOAL := help
|
||||
help: ## Show help
|
||||
@grep -Eh '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
||||
|
||||
.PHONY: lint
|
||||
lint: download ## Lint the repository with golang-ci lint
|
||||
golangci-lint run --max-same-issues 0 --max-issues-per-linter 0 $(if $(CI),--out-format code-climate > gl-code-quality-report.json 2>golangci-stderr-output)
|
||||
|
||||
.PHONY: test
|
||||
test: download ## Run all tests
|
||||
go test -v
|
||||
|
||||
.PHONY: bench
|
||||
bench: download ## Run all benchmarks
|
||||
go test -bench=.
|
||||
|
||||
.PHONY: download
|
||||
download: ## Download dependencies
|
||||
go mod download
|
||||
55
vendor/github.com/matoous/go-nanoid/v2/README.md
generated
vendored
Normal file
55
vendor/github.com/matoous/go-nanoid/v2/README.md
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
# Go Nanoid
|
||||
|
||||
[](https://github.com/matoous/go-nanoid/actions)
|
||||
[](https://godoc.org/github.com/matoous/go-nanoid)
|
||||
[](https://goreportcard.com/report/github.com/matoous/go-nanoid)
|
||||
[](https://github.com/matoous/go-nanoid/issues)
|
||||
[](https://github.com/matoous/go-nanoid/LICENSE)
|
||||
|
||||
This package is Go implementation of [ai's](https://github.com/ai) [nanoid](https://github.com/ai/nanoid)!
|
||||
|
||||
**Safe.** It uses cryptographically strong random generator.
|
||||
|
||||
**Compact.** It uses more symbols than UUID (`A-Za-z0-9_-`)
|
||||
and has the same number of unique options in just 22 symbols instead of 36.
|
||||
|
||||
**Fast.** Nanoid is as fast as UUID but can be used in URLs.
|
||||
|
||||
There's also this alternative: https://github.com/jaevor/go-nanoid.
|
||||
|
||||
## Install
|
||||
|
||||
Via go get tool
|
||||
|
||||
``` bash
|
||||
$ go get github.com/matoous/go-nanoid/v2
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Generate ID
|
||||
|
||||
``` go
|
||||
id, err := gonanoid.New()
|
||||
```
|
||||
|
||||
Generate ID with a custom alphabet and length
|
||||
|
||||
``` go
|
||||
id, err := gonanoid.Generate("abcde", 54)
|
||||
```
|
||||
|
||||
## Notice
|
||||
|
||||
If you use Go Nanoid in your project, please let me know!
|
||||
|
||||
If you have any issues, just feel free and open it in this repository, thanks!
|
||||
|
||||
## Credits
|
||||
|
||||
- [ai](https://github.com/ai) - [nanoid](https://github.com/ai/nanoid)
|
||||
- icza - his tutorial on [random strings in Go](https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang)
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|
||||
108
vendor/github.com/matoous/go-nanoid/v2/gonanoid.go
generated
vendored
Normal file
108
vendor/github.com/matoous/go-nanoid/v2/gonanoid.go
generated
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
package gonanoid
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"math"
|
||||
)
|
||||
|
||||
// defaultAlphabet is the alphabet used for ID characters by default.
|
||||
var defaultAlphabet = []rune("_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
const (
|
||||
defaultSize = 21
|
||||
)
|
||||
|
||||
// getMask generates bit mask used to obtain bits from the random bytes that are used to get index of random character
|
||||
// from the alphabet. Example: if the alphabet has 6 = (110)_2 characters it is sufficient to use mask 7 = (111)_2
|
||||
func getMask(alphabetSize int) int {
|
||||
for i := 1; i <= 8; i++ {
|
||||
mask := (2 << uint(i)) - 1
|
||||
if mask >= alphabetSize-1 {
|
||||
return mask
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Generate is a low-level function to change alphabet and ID size.
|
||||
func Generate(alphabet string, size int) (string, error) {
|
||||
chars := []rune(alphabet)
|
||||
|
||||
if len(alphabet) == 0 || len(alphabet) > 255 {
|
||||
return "", errors.New("alphabet must not be empty and contain no more than 255 chars")
|
||||
}
|
||||
if size <= 0 {
|
||||
return "", errors.New("size must be positive integer")
|
||||
}
|
||||
|
||||
mask := getMask(len(chars))
|
||||
// estimate how many random bytes we will need for the ID, we might actually need more but this is tradeoff
|
||||
// between average case and worst case
|
||||
ceilArg := 1.6 * float64(mask*size) / float64(len(alphabet))
|
||||
step := int(math.Ceil(ceilArg))
|
||||
|
||||
id := make([]rune, size)
|
||||
bytes := make([]byte, step)
|
||||
for j := 0; ; {
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for i := 0; i < step; i++ {
|
||||
currByte := bytes[i] & byte(mask)
|
||||
if currByte < byte(len(chars)) {
|
||||
id[j] = chars[currByte]
|
||||
j++
|
||||
if j == size {
|
||||
return string(id[:size]), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MustGenerate is the same as Generate but panics on error.
|
||||
func MustGenerate(alphabet string, size int) string {
|
||||
id, err := Generate(alphabet, size)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// New generates secure URL-friendly unique ID.
|
||||
// Accepts optional parameter - length of the ID to be generated (21 by default).
|
||||
func New(l ...int) (string, error) {
|
||||
var size int
|
||||
switch {
|
||||
case len(l) == 0:
|
||||
size = defaultSize
|
||||
case len(l) == 1:
|
||||
size = l[0]
|
||||
if size < 0 {
|
||||
return "", errors.New("negative id length")
|
||||
}
|
||||
default:
|
||||
return "", errors.New("unexpected parameter")
|
||||
}
|
||||
bytes := make([]byte, size)
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
id := make([]rune, size)
|
||||
for i := 0; i < size; i++ {
|
||||
id[i] = defaultAlphabet[bytes[i]&63]
|
||||
}
|
||||
return string(id[:size]), nil
|
||||
}
|
||||
|
||||
// Must is the same as New but panics on error.
|
||||
func Must(l ...int) string {
|
||||
id, err := New(l...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
Reference in New Issue
Block a user