Browse Source

Move random string generation funcs to own file

master
Matt Baer 9 years ago
parent
commit
2b7e08e9a1
2 changed files with 24 additions and 10 deletions
  1. +22
    -0
      store/random.go
  2. +2
    -10
      store/store.go

+ 22
- 0
store/random.go View File

@@ -0,0 +1,22 @@
package store

import (
"crypto/rand"
)

func Generate62RandomString(l int) string {
return GenerateRandomString("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", l)
}

func GenerateFriendlyRandomString(l int) string {
return GenerateRandomString("0123456789abcdefghijklmnopqrstuvwxyz", l)
}

func GenerateRandomString(dictionary string, l int) string {
var bytes = make([]byte, l)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}

+ 2
- 10
store/store.go View File

@@ -4,11 +4,10 @@ import (
"os"
"io"
"bytes"
"crypto/rand"
)

const (
nameLen = 12
FriendlyIdLen = 12
)

func SavePost(outDir string, post []byte) (string, error) {
@@ -33,12 +32,5 @@ func SavePost(outDir string, post []byte) (string, error) {
}

func generateFileName() string {
c := nameLen
var dictionary string = "0123456789abcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, c)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
return GenerateFriendlyRandomString(FriendlyIdLen)
}

Loading…
Cancel
Save