Skip to content

Commit 07c364e

Browse files
committed
Hash Tests
1 parent 050b7d4 commit 07c364e

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

encryption.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,23 @@ func Sha1File(v string) string {
107107
return hex.EncodeToString(hash[:])
108108
}
109109

110-
111110
// Hash - Generate a hash value (message digest)
112111
//
113112
// Original: https://www.php.net/manual/en/function.hash.php
114113
//
115114
func Hash(cryp, val string) string {
116115
switch cryp {
117-
case "sha256":
118-
return Sha256(val)
119-
case "sha224":
120-
return Sha224(val)
121-
case "sha384":
122-
return Sha384(val)
123-
case "sha512":
124-
return Sha512(val)
125-
case "sha1":
126-
return Sha1(val)
127-
default:
128-
return MD5(val)
116+
case "sha256":
117+
return Sha256(val)
118+
case "sha224":
119+
return Sha224(val)
120+
case "sha384":
121+
return Sha384(val)
122+
case "sha512":
123+
return Sha512(val)
124+
case "sha1":
125+
return Sha1(val)
126+
default:
127+
return MD5(val)
129128
}
130129
}

tests/Encryption/Hash/Hash_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package phpfuncs
2+
3+
import (
4+
"testing"
5+
6+
"github.com/serkanalgur/phpfuncs"
7+
)
8+
9+
func CoverCheck(t *testing.T, s, exp string) {
10+
if s != exp {
11+
t.Errorf("%q (expected: %q)", s, exp)
12+
}
13+
}
14+
15+
// HashCheckSha1 - Test for Encryption
16+
func HashCheckSha1(t *testing.T) {
17+
want := "0a4d55a8d778e5022fab701977c5d840bbc486d0"
18+
CoverCheck(t, phpfuncs.Hash("sha1","Hello World"), want)
19+
}
20+
21+
// HashCheckSha224 - Test for Encryption
22+
func HashCheckSha224(t *testing.T) {
23+
want := "c4890faffdb0105d991a461e668e276685401b02eab1ef4372795047"
24+
CoverCheck(t, phpfuncs.Hash("sha224","Hello World"), want)
25+
}
26+
27+
// HashCheckSha256 - Test for Encryption
28+
func HashCheckSha256(t *testing.T) {
29+
want := "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"
30+
CoverCheck(t, phpfuncs.Hash("sha256","Hello World"), want)
31+
}
32+
33+
// HashCheckSha384 - Test for Encryption
34+
func HashCheckSha384(t *testing.T) {
35+
want := "99514329186b2f6ae4a1329e7ee6c610a729636335174ac6b740f9028396fcc803d0e93863a7c3d90f86beee782f4f3f"
36+
CoverCheck(t, phpfuncs.Hash("sha384","Hello World"), want)
37+
}
38+
39+
// HashCheckSha512 - Test for Encryption
40+
func HashCheckSha512(t *testing.T) {
41+
want := "2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b"
42+
CoverCheck(t, phpfuncs.Hash("sha512","Hello World"), want)
43+
}
44+
45+
// HashCheckSha512 - Test for Encryption (Expecting to return MD5 Hash)
46+
func HashCheckShaMD5NDefault(t *testing.T) {
47+
want := "b10a8db164e0754105b7a99be72e3fe5"
48+
CoverCheck(t, phpfuncs.Hash("sha12525","Hello World"), want)
49+
}

0 commit comments

Comments
 (0)