Skip to content

Commit

Permalink
Add new tool file referencelib
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiy.safronov committed Jul 28, 2022
1 parent 3c98750 commit f8f991e
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tools/referencelib.go
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
}

0 comments on commit f8f991e

Please sign in to comment.