Skip to content

Commit

Permalink
Merge pull request #135 from mattlorimor/AddCryptoStrings
Browse files Browse the repository at this point in the history
Surface cryptographically secure random strings from goutils
  • Loading branch information
technosophos authored Feb 12, 2019
2 parents 7436748 + 01893d2 commit b70dc08
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
19 changes: 19 additions & 0 deletions docs/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ initials "First Try"

The above returns `FT`

## cryptoRandAlphaNum, cryptoRandAlpha, cryptoRandNumeric, and cryptoRandAscii

These four functions generate cryptographically secure (uses ```crypto/rand```)
random strings, but with different base character sets. They should be used for
generating random strings to be used for security purposes:

- `cryptoRandAlphaNum` uses `0-9a-zA-Z`
- `cryptoRandAlpha` uses `a-zA-Z`
- `cryptoRandNumeric` uses `0-9`
- `cryptoRandAscii` uses all printable ASCII characters

Each of them takes one parameter: the integer length of the string.

```
cryptoRandNumeric 30
```

The above will produce a cryptographically secure random string with thirty digits.

## randAlphaNum, randAlpha, randNumeric, and randAscii

These four functions generate random strings, but with different base character
Expand Down
10 changes: 9 additions & 1 deletion functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
ttemplate "text/template"
"time"

util "github.com/aokoli/goutils"
util "github.com/Masterminds/goutils"
"github.com/huandu/xstrings"
)

Expand Down Expand Up @@ -75,6 +75,10 @@ var nonhermeticFunctions = []string{
"dateModify",

// Strings
"cryptoRandAlphaNum",
"cryptoRandAlpha",
"cryptoRandAscii",
"cryptoRandNumeric",
"randAlphaNum",
"randAlpha",
"randAscii",
Expand Down Expand Up @@ -121,6 +125,10 @@ var genericMap = map[string]interface{}{
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
"nospace": util.DeleteWhiteSpace,
"initials": initials,
"cryptoRandAlphaNum": cryptoRandAlphaNumeric,
"cryptoRandAlpha": cryptoRandAlpha,
"cryptoRandAscii": cryptoRandAscii,
"cryptoRandNumeric": cryptoRandNumeric,
"randAlphaNum": randAlphaNumeric,
"randAlpha": randAlpha,
"randAscii": randAscii,
Expand Down
2 changes: 1 addition & 1 deletion functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"text/template"

"github.com/aokoli/goutils"
"github.com/Masterminds/goutils"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 0 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"strings"

util "github.com/aokoli/goutils"
util "github.com/Masterminds/goutils"
)

func base64encode(v string) string {
Expand Down Expand Up @@ -55,6 +55,26 @@ func initials(s string) string {
return util.Initials(s)
}

func cryptoRandAlphaNumeric(count int) string {
r, _ := util.CryptoRandomAlphaNumeric(count)
return r
}

func cryptoRandAlpha(count int) string {
r, _ := util.CryptoRandomAlphabetic(count)
return r
}

func cryptoRandAscii(count int) string {
r, _ := util.CryptoRandomAscii(count)
return r
}

func cryptoRandNumeric(count int) string {
r, _ := util.CryptoRandomNumeric(count)
return r
}

func randAlphaNumeric(count int) string {
// It is not possible, it appears, to actually generate an error here.
r, _ := util.RandomAlphaNumeric(count)
Expand Down
27 changes: 26 additions & 1 deletion strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"fmt"
"math/rand"
"testing"
"unicode/utf8"

"github.com/aokoli/goutils"
"github.com/Masterminds/goutils"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -189,6 +190,30 @@ func TestGoutils(t *testing.T) {
}
}

func TestCryptoRandom(t *testing.T) {
// These tests have no predictable character sequence. No checks for exact string output are necessary.

// {{cryptoRandAlphaNum 5}} should yield five random characters
if x, _ := runRaw(`{{cryptoRandAlphaNum 5}}`, nil); utf8.RuneCountInString(x) != 5 {
t.Errorf("String should be 5 characters; string was %v characters", utf8.RuneCountInString(x))
}

// {{cryptoRandAlpha 5}} should yield five random characters
if x, _ := runRaw(`{{cryptoRandAlpha 5}}`, nil); utf8.RuneCountInString(x) != 5 {
t.Errorf("String should be 5 characters; string was %v characters", utf8.RuneCountInString(x))
}

// {{cryptoRandAscii 5}} should yield five random characters
if x, _ := runRaw(`{{cryptoRandAscii 5}}`, nil); utf8.RuneCountInString(x) != 5 {
t.Errorf("String should be 5 characters; string was %v characters", utf8.RuneCountInString(x))
}

// {{cryptoRandNumeric 5}} should yield five random characters
if x, _ := runRaw(`{{cryptoRandNumeric 5}}`, nil); utf8.RuneCountInString(x) != 5 {
t.Errorf("String should be 5 characters; string was %v characters", utf8.RuneCountInString(x))
}
}

func TestRandom(t *testing.T) {
// One of the things I love about Go:
goutils.RANDOM = rand.New(rand.NewSource(1))
Expand Down

0 comments on commit b70dc08

Please sign in to comment.