referencing issue #57

This commit is contained in:
George Shaw 2017-11-06 11:43:26 -06:00
parent 8a2f43559f
commit 08b94c3a4c
3 changed files with 45 additions and 15 deletions

View file

@ -47,11 +47,7 @@ func Auth(res http.ResponseWriter, req *http.Request) bool {
return false
}
secret, err := encryption.RandomString()
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
secret := encryption.RandomString(16)
userDatabaseData.Secret = secret
err = ds.Put(database.BUCKET_USERS, []byte(userRequestData.User), userDatabaseData)

View file

@ -2,18 +2,22 @@
package encryption
import (
"crypto/rand"
"fmt"
"math/rand"
"time"
)
func RandomString() (string, error) {
n := 5
b := make([]byte, n)
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" +
"1234567890!@#$%^&*()"
if _, err := rand.Read(b); err != nil {
return "", err
var seed *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
// RandomString function takes an integer length value in and returns a
// random string of that size built from the charset constant.
func RandomString(length int) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seed.Intn(len(charset))]
}
s := fmt.Sprintf("%X", b)
return s, nil
return string(b)
}

View file

@ -0,0 +1,30 @@
// Encryption package has functions inside of it that utilize various encypting and hashing techniques
package encryption
import "testing"
func TestRandomString(t *testing.T) {
testData := []struct {
length int
output string
}{
{16, ""},
{16, ""},
{16, ""},
{16, ""},
}
for i := 0; i < len(testData); i++ {
testData[i].output = RandomString(testData[i].length)
}
for i := 0; i < len(testData)-1; i++ {
compare := testData[i].output
for ii := i + 1; ii < len(testData); ii++ {
if compare == testData[ii].output {
t.Errorf("Random string generator generated two strings with the same value. (%s - %s)", compare, testData[ii].output)
}
}
}
}