Initial open source release

This commit is contained in:
Ronni Skansing committed 2025-08-21 16:14:09 +02:00
commit 11cf01f08e
488 files changed
+97180

No files matched your search

+40
View File
@@ -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
}
+17
View File
@@ -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))
}
})
}