Skip to content

Commit 229fcb5

Browse files
authored
Merge pull request #20 from serkanalgur:development
Hash Function, Tests and Gofmt
2 parents 4fe9893 + 03be6f5 commit 229fcb5

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

encryption.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package phpfuncs
33
import (
44
"crypto/md5"
55
"crypto/sha1"
6+
"crypto/sha256"
7+
"crypto/sha512"
68
"encoding/base64"
79
"encoding/hex"
810
"io/ioutil"
@@ -62,6 +64,34 @@ func Sha1(v string) string {
6264
return hex.EncodeToString(hash[:])
6365
}
6466

67+
// Sha224 - Calculate the sha1 hash of a string.
68+
//
69+
func Sha224(v string) string {
70+
hash := sha256.Sum224([]byte(v))
71+
return hex.EncodeToString(hash[:])
72+
}
73+
74+
// Sha256 - Calculate the sha1 hash of a string.
75+
//
76+
func Sha256(v string) string {
77+
hash := sha256.Sum256([]byte(v))
78+
return hex.EncodeToString(hash[:])
79+
}
80+
81+
// Sha384 - Calculate the sha1 hash of a string.
82+
//
83+
func Sha384(v string) string {
84+
hash := sha512.Sum384([]byte(v))
85+
return hex.EncodeToString(hash[:])
86+
}
87+
88+
// Sha512 - Calculate the sha1 hash of a string.
89+
//
90+
func Sha512(v string) string {
91+
hash := sha512.Sum512([]byte(v))
92+
return hex.EncodeToString(hash[:])
93+
}
94+
6595
// Sha1File - Calculate the sha1 hash of a file.
6696
//
6797
// Original : https://www.php.net/manual/en/function.sha1-file.php
@@ -76,3 +106,24 @@ func Sha1File(v string) string {
76106
hash := sha1.Sum(file)
77107
return hex.EncodeToString(hash[:])
78108
}
109+
110+
// Hash - Generate a hash value (message digest)
111+
//
112+
// Original: https://www.php.net/manual/en/function.hash.php
113+
//
114+
func Hash(cryp, val string) string {
115+
switch cryp {
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)
128+
}
129+
}

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)