@ -1,9 +1,11 @@
package store
package store
import (
import (
"crypto/rand"
"encoding/json"
"encoding/json"
"math/rand "
"math/big "
"time"
"time"
"unicode"
"github.com/boltdb/bolt"
"github.com/boltdb/bolt"
"github.com/pkg/errors"
"github.com/pkg/errors"
@ -40,7 +42,10 @@ func (s *Store) createEntryRaw(key, value []byte) error {
// createEntry creates a new entry
// createEntry creates a new entry
func ( s * Store ) createEntry ( URL , remoteAddr string ) ( string , error ) {
func ( s * Store ) createEntry ( URL , remoteAddr string ) ( string , error ) {
id := generateRandomString ( s . idLength )
id , err := generateRandomString ( s . idLength )
if err != nil {
return "" , errors . Wrap ( err , "could not generate random string" )
}
exists := s . checkExistence ( id )
exists := s . checkExistence ( id )
if ! exists {
if ! exists {
raw , err := json . Marshal ( Entry {
raw , err := json . Marshal ( Entry {
@ -57,11 +62,19 @@ func (s *Store) createEntry(URL, remoteAddr string) (string, error) {
}
}
// generateRandomString generates a random string with an predefined length
// generateRandomString generates a random string with an predefined length
func generateRandomString ( length uint ) string {
func generateRandomString ( length uint ) ( string , error ) {
letterRunes := [ ] rune ( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
var result string
b := make ( [ ] rune , length )
for len ( result ) < int ( length ) {
for i := range b {
num , err := rand . Int ( rand . Reader , big . NewInt ( int64 ( 127 ) ) )
b [ i ] = letterRunes [ rand . Intn ( len ( letterRunes ) ) ]
if err != nil {
return "" , err
}
n := num . Int64 ( )
// Make sure that the number/byte/letter is inside
// the range of printable ASCII characters (excluding space and DEL)
if unicode . IsLetter ( rune ( n ) ) {
result += string ( n )
}
}
}
return string ( b )
return result , nil
}
}