services
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
|
||||
vendor/
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2010-2018 Google, Inc. http://angularjs.org
|
||||
|
||||
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.
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
# Randstr
|
||||
# [](http://godoc.org/github.com/thanhpk/randstr)
|
||||
|
||||
Randstr is an Go library for generating secure random strings
|
||||
|
||||
## Install
|
||||
```
|
||||
go get -u github.com/thanhpk/randstr
|
||||
```
|
||||
|
||||
## Usage
|
||||
### Generate a random hex string
|
||||
```go
|
||||
token := randstr.Hex(16) // generate 128-bit hex string
|
||||
```
|
||||
Running example
|
||||
```go
|
||||
package main
|
||||
import(
|
||||
"github.com/thanhpk/randstr"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for i := 0; i < 5; i++ {
|
||||
token := randstr.Hex(16) // generate 128-bit hex string
|
||||
fmt.Println(token)
|
||||
}
|
||||
}
|
||||
// Output:
|
||||
// 67aab2d956bd7cc621af22cfb169cba8
|
||||
// 226eeb52947edbf3e97d1e6669e212c2
|
||||
// 5f3615e95d103d14ffb5b655aa0eec1e
|
||||
// ff3ab4efbd74025b87b14b59422d304c
|
||||
// a6705813c174ca73ed795ea0bab12726
|
||||
```
|
||||
|
||||
### Generate a random ASCII string
|
||||
```go
|
||||
token := randstr.String(16) // generate a random 16 character length string
|
||||
```
|
||||
Running example
|
||||
```go
|
||||
package main
|
||||
import(
|
||||
"github.com/thanhpk/randstr"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for i := 0; i < 5; i++ {
|
||||
token := randstr.String(16)
|
||||
fmt.Println(token)
|
||||
}
|
||||
}
|
||||
// Output:
|
||||
// 7EbxkrHc1l3Ahmyr
|
||||
// I5XH2gc1EEHgbmGI
|
||||
// GlCycMpsxGkn9cDQ
|
||||
// U2OfBDQoak0z8FwV
|
||||
// kDX1m81u14YwEiCY
|
||||
```
|
||||
|
||||
## License [](https://opensource.org/licenses/MIT)
|
||||
MIT
|
||||
+1
@@ -0,0 +1 @@
|
||||
module github.com/thanhpk/randstr
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
// Package randstr provides basic functions for generating random bytes, string
|
||||
package randstr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// Bytes generates n random bytes
|
||||
func Bytes(n int) []byte {
|
||||
b := make([]byte, n)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Base64 generates a random base64 string with length of n
|
||||
func Base64(n int) string {
|
||||
return String(n, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/")
|
||||
}
|
||||
|
||||
// Base64 generates a random base62 string with length of n
|
||||
func Base62(s int) string {
|
||||
return String(s, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
}
|
||||
|
||||
// Hex generates a random hex string with length of n
|
||||
// e.g: 67aab2d956bd7cc621af22cfb169cba8
|
||||
func Hex(n int) string { return hex.EncodeToString(Bytes(n)) }
|
||||
|
||||
// list of default letters that can be used to make a random string when calling String
|
||||
// function with no letters provided
|
||||
var defLetters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
// String generates a random string using only letters provided in the letters parameter
|
||||
// if user ommit letters parameters, this function will use defLetters instead
|
||||
func String(n int, letters ...string) string {
|
||||
var letterRunes []rune
|
||||
if len(letters) == 0 {
|
||||
letterRunes = defLetters
|
||||
} else {
|
||||
letterRunes = []rune(letters[0])
|
||||
}
|
||||
|
||||
var bb bytes.Buffer
|
||||
bb.Grow(n)
|
||||
l := uint32(len(letterRunes))
|
||||
// on each loop, generate one random rune and append to output
|
||||
for i := 0; i < n; i++ {
|
||||
bb.WriteRune(letterRunes[binary.BigEndian.Uint32(Bytes(4))%l])
|
||||
}
|
||||
return bb.String()
|
||||
}
|
||||
Reference in New Issue
Block a user