Skip to content

Commit 050b7d4

Browse files
committed
Encryption Sha Series & Hash function
1 parent 4fe9893 commit 050b7d4

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

encryption.go

Lines changed: 52 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,25 @@ func Sha1File(v string) string {
76106
hash := sha1.Sum(file)
77107
return hex.EncodeToString(hash[:])
78108
}
109+
110+
111+
// Hash - Generate a hash value (message digest)
112+
//
113+
// Original: https://www.php.net/manual/en/function.hash.php
114+
//
115+
func Hash(cryp, val string) string {
116+
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)
129+
}
130+
}

0 commit comments

Comments
 (0)