Skip to content

Commit 10dfe36

Browse files
authored
Merge pull request #9 for v0.7 Release
2 parents 53609e7 + 63371a7 commit 10dfe36

File tree

7 files changed

+105
-37
lines changed

7 files changed

+105
-37
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ branches:
1111

1212
go:
1313
- 1.8
14-
- 1.8.x
14+
- 1.9
1515
- tip
1616

1717
go_import_path: aahframework.org/essentials.v0
1818

19+
install:
20+
- git config --global http.https://aahframework.org.followRedirects true
21+
- go get -t -v ./...
22+
1923
script:
2024
- bash <(curl -s https://aahframework.org/go-test)
2125

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
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.6-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.7-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.6 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Jul 22, 2017***
5+
***v0.7 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Sep 29, 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:
8-
* filepath
9-
* GUID (Globally Unique Identifier)
10-
* random string, random byte generation at fixed length
11-
* go
12-
* io
13-
* os
14-
* reflect
15-
* string
16-
* archive (zip)
8+
9+
* filepath
10+
* GUID (Globally Unique Identifier - Mongo Object ID algorithm)
11+
* random string, random byte generation at fixed length
12+
* go
13+
* io
14+
* os
15+
* reflect
16+
* string
17+
* encode
18+
* archive (zip)
1719

1820
*`essentials` developed for aah framework. However, it's an independent library, can be used separately with any `Go` language project. Feel free to use it.*
1921

encode.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
"encoding/base64"
9+
"errors"
10+
)
11+
12+
// ErrBase64Decode returned when given string unable to do base64 decode.
13+
var ErrBase64Decode = errors.New("encoding/base64: decode error")
14+
15+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
16+
// Encode/Decode Base64 methods
17+
//___________________________________
18+
19+
// EncodeToBase64 method encodes given bytes into base64 bytes.
20+
// Reference: https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L169
21+
func EncodeToBase64(v []byte) []byte {
22+
encoded := make([]byte, base64.URLEncoding.EncodedLen(len(v)))
23+
base64.URLEncoding.Encode(encoded, v)
24+
return encoded
25+
}
26+
27+
// DecodeBase64 method decodes given base64 into bytes.
28+
// Reference: https://github.com/golang/go/blob/master/src/encoding/base64/base64.go#L384
29+
func DecodeBase64(v []byte) ([]byte, error) {
30+
decoded := make([]byte, base64.URLEncoding.DecodedLen(len(v)))
31+
b, err := base64.URLEncoding.Decode(decoded, v)
32+
if err != nil {
33+
return nil, ErrBase64Decode
34+
}
35+
return decoded[:b], nil
36+
}

encode_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 TestEncodeBase64(t *testing.T) {
14+
value := "this is gonna be encoded in base64"
15+
16+
valueBytes := EncodeToBase64([]byte(value))
17+
assert.NotNil(t, valueBytes)
18+
19+
decoded, err := DecodeBase64(valueBytes)
20+
assert.Nil(t, err)
21+
assert.NotNil(t, decoded)
22+
23+
_, err = DecodeBase64([]byte("dGhpcyBpcyBnb25uYSBiZSBlbmGVkIGluIGJhc2U2NA=="))
24+
assert.NotNil(t, err)
25+
assert.Equal(t, ErrBase64Decode, err)
26+
}

random_key.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,41 @@ var (
2525
mr *sync.Mutex
2626
)
2727

28-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
29-
// Random String methods
30-
//___________________________________
28+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
29+
// Random String methods (Secure and Math)
30+
//_________________________________________
3131

32-
// RandomString method generates the random string for given length using
32+
// SecureRandomString method generates the random string for given length using
3333
// `crypto/rand`.
34-
func RandomString(length int) string {
35-
return hex.EncodeToString(GenerateRandomKey(length / 2))
34+
func SecureRandomString(length int) string {
35+
return hex.EncodeToString(GenerateSecureRandomKey(length / 2))
3636
}
3737

38-
// RandomStringbm method generates the random string for given length using
38+
// RandomString method generates the random string for given length using
3939
// `math/rand.Source` and byte mask.
40-
func RandomStringbm(length int) string {
41-
return string(GenerateRandomKeybm(length))
40+
func RandomString(length int) string {
41+
return string(GenerateRandomKey(length))
4242
}
4343

44-
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
45-
// Random key methods
46-
//___________________________________
44+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
45+
// Random Key Methods (Secure and Math)
46+
//_______________________________________
4747

48-
// GenerateRandomKey method generates the random bytes for given length using
48+
// GenerateSecureRandomKey method generates the random bytes for given length using
4949
// `crypto/rand`.
50-
func GenerateRandomKey(length int) []byte {
50+
func GenerateSecureRandomKey(length int) []byte {
5151
k := make([]byte, length)
5252
if _, err := io.ReadFull(rand.Reader, k); err != nil {
5353
// fallback to math based random key generater
54-
return GenerateRandomKeybm(length)
54+
return GenerateRandomKey(length)
5555
}
5656
return k
5757
}
5858

59-
// GenerateRandomKeybm method generates the random bytes for given length using
59+
// GenerateRandomKey method generates the random bytes for given length using
6060
// `math/rand.Source` and byte mask.
6161
// StackOverflow Ref - http://stackoverflow.com/a/31832326
62-
func GenerateRandomKeybm(length int) []byte {
62+
func GenerateRandomKey(length int) []byte {
6363
b := make([]byte, length)
6464
// A randSrc() generates 63 random bits, enough for letterIdxMax characters!
6565
for i, cache, remain := length-1, randSrc(), letterIdxMax; i >= 0; {

random_key_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,33 @@ import (
1111
)
1212

1313
func TestEssRandomKey(t *testing.T) {
14-
key1 := GenerateRandomKey(32)
14+
key1 := GenerateSecureRandomKey(32)
1515
assert.NotNil(t, key1)
1616
assert.True(t, len(key1) == 32)
1717

18-
key2 := GenerateRandomKeybm(64)
18+
key2 := GenerateRandomKey(64)
1919
assert.NotNil(t, key2)
2020
assert.True(t, len(key2) == 64)
2121
}
2222

2323
func TestEssRandomString(t *testing.T) {
24-
str1 := RandomString(32)
24+
str1 := SecureRandomString(32)
2525
assert.True(t, len(str1) == 32)
2626
assert.NotNil(t, str1)
2727

28-
str2 := RandomStringbm(32)
28+
str2 := RandomString(32)
2929
assert.True(t, len(str2) == 32)
3030
assert.NotNil(t, str2)
3131
}
3232

3333
func BenchmarkGenerateRandomKey(b *testing.B) {
3434
for i := 0; i < b.N; i++ {
35-
GenerateRandomKey(16)
35+
GenerateSecureRandomKey(16)
3636
}
3737
}
3838

39-
func BenchmarkGenerateRandomKeybm(b *testing.B) {
39+
func BenchmarkGenerateMathRandomKey(b *testing.B) {
4040
for i := 0; i < b.N; i++ {
41-
GenerateRandomKeybm(32)
41+
GenerateRandomKey(32)
4242
}
4343
}

essentials.go renamed to version.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.6"
10+
var Version = "0.7"

0 commit comments

Comments
 (0)