-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
141 additions
and
186 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package convutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestS2bAndB2s(t *testing.T) { | ||
r := "s2b and b2s" | ||
b := S2b(r) | ||
s := B2s(b) | ||
|
||
assert.Equal(t, []byte(r), b) | ||
assert.Equal(t, r, s) | ||
} | ||
|
||
func TestNextNumberPowerOf2(t *testing.T) { | ||
assert.Equal(t, uint64(16), NextNumberPowerOf2(10)) | ||
assert.Equal(t, uint64(32), NextNumberPowerOf2(20)) | ||
assert.Equal(t, uint64(32), NextNumberPowerOf2(30)) | ||
assert.Equal(t, uint64(64), NextNumberPowerOf2(40)) | ||
assert.Equal(t, uint64(128), NextNumberPowerOf2(100)) | ||
assert.Equal(t, uint64(256), NextNumberPowerOf2(200)) | ||
assert.Equal(t, uint64(512), NextNumberPowerOf2(500)) | ||
assert.Equal(t, uint64(1024), NextNumberPowerOf2(1000)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
package cryptoutil | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/md5" | ||
"crypto/sha1" | ||
"encoding/hex" | ||
) | ||
|
||
// Md5Hex | ||
func Md5Hex(str string) string { | ||
hbs := md5.Sum([]byte(str)) | ||
return hex.EncodeToString(hbs[:]) | ||
} | ||
|
||
// Md5HexShort | ||
func Md5HexShort(str string) string { | ||
hbs := md5.Sum([]byte(str)) | ||
return hex.EncodeToString(hbs[4:12]) | ||
} | ||
|
||
// HmacHex | ||
func HmacHex(data, key []byte) string { | ||
hmacSha1 := hmac.New(sha1.New, key) | ||
hmacSha1.Write(data) | ||
return hex.EncodeToString(hmacSha1.Sum(nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cryptoutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHmacHex(t *testing.T) { | ||
hex := HmacHex([]byte("123"), []byte("key")) | ||
assert.Equal(t, "d4a5b6721d75a5ac15ec698818c77fe1f6e40187", hex) | ||
} | ||
|
||
func TestMd5Hex(t *testing.T) { | ||
hex := Md5Hex("123") | ||
assert.Equal(t, "202cb962ac59075b964b07152d234b70", hex) | ||
} | ||
|
||
func TestMd5HexShort(t *testing.T) { | ||
hex := Md5HexShort("123") | ||
assert.Equal(t, "ac59075b964b0715", hex) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package strutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStrInSlice(t *testing.T) { | ||
s := []string{"test1", "test2", "test3", "test4", "test5"} | ||
assert.False(t, StrInSlice("test", s)) | ||
assert.True(t, StrInSlice("test3", s)) | ||
} | ||
|
||
func TestBoolFromStr(t *testing.T) { | ||
for _, trueString := range TrueStrings { | ||
assert.True(t, BoolFromStr(trueString, false)) | ||
} | ||
|
||
for _, trueString := range FalseStrings { | ||
assert.False(t, BoolFromStr(trueString, true)) | ||
} | ||
|
||
assert.True(t, BoolFromStr("ok1", true)) | ||
assert.False(t, BoolFromStr("ok1", false)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.