Skip to content

Commit f636e30

Browse files
authored
Merge pull request #6 for v0.5 Release
2 parents 45d419f + a3d8131 commit f636e30

File tree

5 files changed

+186
-3
lines changed

5 files changed

+186
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# essentials - aah framework
22
[![Build Status](https://travis-ci.org/go-aah/essentials.svg?branch=master)](https://travis-ci.org/go-aah/essentials) [![codecov](https://codecov.io/gh/go-aah/essentials/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/essentials/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/essentials.v0)](https://goreportcard.com/report/aahframework.org/essentials.v0)
3-
[![Version](https://img.shields.io/badge/version-0.4-blue.svg)](https://github.com/go-aah/essentials/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/essentials.v0?status.svg)](https://godoc.org/aahframework.org/essentials.v0) [![License](https://img.shields.io/github/license/go-aah/essentials.svg)](LICENSE)
3+
[![Version](https://img.shields.io/badge/version-0.5-blue.svg)](https://github.com/go-aah/essentials/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/essentials.v0?status.svg)](https://godoc.org/aahframework.org/essentials.v0) [![License](https://img.shields.io/github/license/go-aah/essentials.svg)](LICENSE)
44

5-
***v0.4 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Mar 30, 2017***
5+
***v0.5 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Apr 07, 2017***
66

77
`essentials` contains simple & useful utils methods for Go. aah framework utilizes essentials (aka `ess`) library across. Essentials library complements with handy methods, refer godoc to know more about methods:
88
* filepath
99
* GUID (Globally Unique Identifier)
10+
* random string, random byte generation at fixed length
1011
* go
1112
* io
1213
* os

archive_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import (
8+
"io/ioutil"
9+
"testing"
10+
11+
"aahframework.org/test.v0/assert"
12+
)
13+
14+
func TestArchiveZip(t *testing.T) {
15+
// Prepare data for zip file
16+
testdataPath := getTestdataPath()
17+
path1 := join(testdataPath, "dirpaths", "level1", "level2", "level3")
18+
path11 := join(testdataPath, "dirpaths", "level1", "level1-1")
19+
path12 := join(testdataPath, "dirpaths", "level1", "level1-2")
20+
path21 := join(testdataPath, "dirpaths", "level1", "level2", "level2-1")
21+
path22 := join(testdataPath, "dirpaths", "level1", "level2", "level2-2")
22+
defer DeleteFiles(join(testdataPath, "dirpaths"))
23+
24+
_ = MkDirAll(path1, 0755)
25+
_ = MkDirAll(path11, 0755)
26+
_ = MkDirAll(path12, 0755)
27+
_ = MkDirAll(path21, 0755)
28+
_ = MkDirAll(path22, 0755)
29+
30+
_ = ioutil.WriteFile(join(path1, "file1.txt"), []byte("file1.txt"), 0600)
31+
_ = ioutil.WriteFile(join(path11, "file11.txt"), []byte("file11.txt"), 0600)
32+
_ = ioutil.WriteFile(join(path12, "file12.txt"), []byte("file12.txt"), 0600)
33+
_ = ioutil.WriteFile(join(path21, "file21.txt"), []byte("file21.txt"), 0600)
34+
_ = ioutil.WriteFile(join(path22, "file22.txt"), []byte("file22.txt"), 0600)
35+
36+
zipName := join(testdataPath, "testarchive.zip")
37+
defer DeleteFiles(zipName)
38+
39+
err := Zip(zipName, join(testdataPath, "dirpaths"))
40+
assert.Nil(t, err)
41+
assert.True(t, IsFileExists(zipName))
42+
}

essentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
package ess
88

99
// Version no. of essentials library
10-
var Version = "0.4"
10+
var Version = "0.5"

random_key.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import (
8+
"crypto/rand"
9+
"encoding/hex"
10+
"io"
11+
mrand "math/rand"
12+
"sync"
13+
"time"
14+
)
15+
16+
const (
17+
letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
18+
letterIdxBits = 6 // 6 bits to represent a letter index
19+
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
20+
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
21+
)
22+
23+
var (
24+
mRandSrc mrand.Source
25+
mr *sync.Mutex
26+
)
27+
28+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
29+
// Random String methods
30+
//___________________________________
31+
32+
// RandomString method generates the random string for given length using
33+
// `crypto/rand`.
34+
func RandomString(length int) string {
35+
return hex.EncodeToString(GenerateRandomKey(length / 2))
36+
}
37+
38+
// RandomStringbm method generates the random string for given length using
39+
// `math/rand.Source` and byte mask.
40+
func RandomStringbm(length int) string {
41+
return string(GenerateRandomKeybm(length))
42+
}
43+
44+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
45+
// Random key methods
46+
//___________________________________
47+
48+
// GenerateRandomKey method generates the random bytes for given length using
49+
// `crypto/rand`.
50+
func GenerateRandomKey(length int) []byte {
51+
k := make([]byte, length)
52+
if _, err := io.ReadFull(rand.Reader, k); err != nil {
53+
// fallback to math based random key generater
54+
return GenerateRandomKeybm(length)
55+
}
56+
return k
57+
}
58+
59+
// GenerateRandomKeybm method generates the random bytes for given length using
60+
// `math/rand.Source` and byte mask.
61+
// StackOverflow Ref - http://stackoverflow.com/a/31832326
62+
func GenerateRandomKeybm(length int) []byte {
63+
b := make([]byte, length)
64+
// A randSrc() generates 63 random bits, enough for letterIdxMax characters!
65+
for i, cache, remain := length-1, randSrc(), letterIdxMax; i >= 0; {
66+
if remain == 0 {
67+
cache, remain = randSrc(), letterIdxMax
68+
}
69+
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
70+
b[i] = letterBytes[idx]
71+
i--
72+
}
73+
cache >>= letterIdxBits
74+
remain--
75+
}
76+
77+
return b
78+
}
79+
80+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
81+
// Unexported methods
82+
//___________________________________
83+
84+
func randSrc() int64 {
85+
mr.Lock()
86+
defer mr.Unlock()
87+
return mRandSrc.Int63()
88+
}
89+
90+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
91+
// init
92+
//___________________________________
93+
94+
func init() {
95+
mRandSrc = mrand.NewSource(time.Now().UnixNano())
96+
mr = &sync.Mutex{}
97+
}

random_key_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import (
8+
"testing"
9+
10+
"aahframework.org/test.v0/assert"
11+
)
12+
13+
func TestEssRandomKey(t *testing.T) {
14+
key1 := GenerateRandomKey(32)
15+
assert.NotNil(t, key1)
16+
assert.True(t, len(key1) == 32)
17+
18+
key2 := GenerateRandomKeybm(64)
19+
assert.NotNil(t, key2)
20+
assert.True(t, len(key2) == 64)
21+
}
22+
23+
func TestEssRandomString(t *testing.T) {
24+
str1 := RandomString(32)
25+
assert.True(t, len(str1) == 32)
26+
assert.NotNil(t, str1)
27+
28+
str2 := RandomStringbm(32)
29+
assert.True(t, len(str2) == 32)
30+
assert.NotNil(t, str2)
31+
}
32+
33+
func BenchmarkGenerateRandomKey(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
GenerateRandomKey(16)
36+
}
37+
}
38+
39+
func BenchmarkGenerateRandomKeybm(b *testing.B) {
40+
for i := 0; i < b.N; i++ {
41+
GenerateRandomKeybm(32)
42+
}
43+
}

0 commit comments

Comments
 (0)