-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sergiy.safronov
committed
Jul 28, 2022
1 parent
3c98750
commit f8f991e
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* ========================================================================== | ||
Golib Repo | ||
Filename: referencelib.go | ||
Owner: Sergiy Safronov | ||
Source : github.com/CoderSergiy/golib/tools | ||
Purpose: Methods to generate random reference | ||
============================================================================= | ||
*/ | ||
|
||
package tools | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
) | ||
|
||
/**************************************************************************************** | ||
* | ||
* Function : GenerateRandomBytes | ||
* | ||
* Purpose : Generate Random Bytes for reference purpose | ||
* | ||
* Input : n int - specify size of the buffer | ||
* | ||
* Return : []]byte - securely generated random bytes | ||
* error - if happened, nil otherwise | ||
*/ | ||
func GenerateRandomBytes(n int) ([]byte, error) { | ||
b := make([]byte, n) | ||
_, err := rand.Read(b) | ||
// Note that err == nil only if we read len(b) bytes. | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return b, nil | ||
} | ||
|
||
/**************************************************************************************** | ||
* | ||
* Function : GenerateRandomString | ||
* | ||
* Purpose : Generate Random String for reference purpose | ||
* | ||
* Input : n int - size of the returning string | ||
* | ||
* Return : string - securely generated random string | ||
* error - if happened, nil otherwise | ||
*/ | ||
func GenerateRandomString(n int) (string, error) { | ||
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" | ||
bytes, err := GenerateRandomBytes(n) | ||
if err != nil { | ||
return "", err | ||
} | ||
for i, b := range bytes { | ||
bytes[i] = letters[b%byte(len(letters))] | ||
} | ||
return string(bytes), nil | ||
} | ||
|
||
/**************************************************************************************** | ||
* | ||
* Function : GenerateRandomStringURLSafe | ||
* | ||
* Purpose : Generate URL-safe, base64 encoded random string | ||
* | ||
* Input : n int - size of the returning string | ||
* | ||
* Return : string - securely generated random bytes | ||
* error - if happened, nil otherwise | ||
*/ | ||
func GenerateRandomStringURLSafe(n int) (string, error) { | ||
b, err := GenerateRandomBytes(n) | ||
return base64.URLEncoding.EncodeToString(b), err | ||
} |