mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-10-11 01:28:57 +02:00
Initial open source release
This commit is contained in:
commit
11cf01f08e
488 files changed
+97180
No files matched your search
@@ -0,0 +1,40 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/phishingclub/phishingclub/errs"
|
||||
)
|
||||
|
||||
// GenerateRandomLowerAndUpper generates random lower and upper case string
|
||||
func GenerateRandomURLBase64Encoded(length int) (string, error) {
|
||||
randomBytes, err := GenerateRandomBytes(length)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate random string with random bytes: %w", err)
|
||||
}
|
||||
str := base64.URLEncoding.EncodeToString(randomBytes)
|
||||
return str[:length], nil
|
||||
}
|
||||
|
||||
// GenerateRandomBytes generates random bytes
|
||||
func GenerateRandomBytes(length int) ([]byte, error) {
|
||||
buff := make([]byte, length)
|
||||
_, err := rand.Read(buff)
|
||||
if err != nil {
|
||||
return []byte{}, fmt.Errorf("failed to generate random bytes: %w", err)
|
||||
}
|
||||
return buff, nil
|
||||
}
|
||||
|
||||
// RandomIntN generates a random number between 0 and n
|
||||
func RandomIntN(n int) (int, error) {
|
||||
max := big.NewInt(int64(n))
|
||||
randNum, err := rand.Int(rand.Reader, max)
|
||||
if err != nil {
|
||||
return 0, errs.Wrap(err)
|
||||
}
|
||||
return int(randNum.Int64()), nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package random
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGenerateRandomLowerUpperAndNumeric(t *testing.T) {
|
||||
t.Run("should generate a random password of expected length", func(t *testing.T) {
|
||||
length := 10
|
||||
password, err := GenerateRandomURLBase64Encoded(length)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
if len(password) != length {
|
||||
t.Errorf("expected password length to be %d, got %d", length, len(password))
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user