-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathencryption.go
129 lines (115 loc) · 3.16 KB
/
encryption.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package phpfuncs
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"io/ioutil"
)
// Base64Encode - Encodes data with MIME base64.
//
// Original : https://www.php.net/manual/en/function.base64-encode.php
//
// Encodes given data with base64.
func Base64Encode(v string) string {
return base64.StdEncoding.EncodeToString([]byte(v))
}
// Base64Decode - Decodes data encoded with MIME base64.
//
// Original : https://www.php.net/manual/en/function.base64-decode.php
//
// Decodes a base64 encoded data.
func Base64Decode(v string) (string, error) {
Base64, Err := base64.StdEncoding.DecodeString(v)
return string(Base64), Err
}
// MD5 - Calculate the md5 hash of a string.
//
// Original : https://www.php.net/manual/en/function.md5.php
//
// Calculates the MD5 hash of str using the RSA Data Security, Inc. MD5 Message-Digest Algorithm.
func MD5(v string) string {
hash := md5.Sum([]byte(v))
return hex.EncodeToString(hash[:])
}
// MD5File - Calculates the md5 hash of a given file.
//
// Original : https://www.php.net/manual/en/function.md5-file.php
//
// Calculates the MD5 hash of the file specified by the filename parameter using the RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. The hash is a 32-character hexadecimal number.
func MD5File(v string) string {
file, err := ioutil.ReadFile(v)
if err != nil {
return ""
}
hash := md5.Sum(file)
return hex.EncodeToString(hash[:])
}
// Sha1 - Calculate the sha1 hash of a string.
//
// Original : https://www.php.net/manual/en/function.sha1.php
//
// Calculates the sha1 hash of str using the US Secure Hash Algorithm 1.
func Sha1(v string) string {
hash := sha1.Sum([]byte(v))
return hex.EncodeToString(hash[:])
}
// Sha224 - Calculate the sha1 hash of a string.
//
func Sha224(v string) string {
hash := sha256.Sum224([]byte(v))
return hex.EncodeToString(hash[:])
}
// Sha256 - Calculate the sha1 hash of a string.
//
func Sha256(v string) string {
hash := sha256.Sum256([]byte(v))
return hex.EncodeToString(hash[:])
}
// Sha384 - Calculate the sha1 hash of a string.
//
func Sha384(v string) string {
hash := sha512.Sum384([]byte(v))
return hex.EncodeToString(hash[:])
}
// Sha512 - Calculate the sha1 hash of a string.
//
func Sha512(v string) string {
hash := sha512.Sum512([]byte(v))
return hex.EncodeToString(hash[:])
}
// Sha1File - Calculate the sha1 hash of a file.
//
// Original : https://www.php.net/manual/en/function.sha1-file.php
//
// Calculates the sha1 hash of the file specified by filename using the US Secure Hash Algorithm 1, and returns that hash. The hash is a 40-character hexadecimal number.
func Sha1File(v string) string {
file, err := ioutil.ReadFile(v)
if err != nil {
return ""
}
hash := sha1.Sum(file)
return hex.EncodeToString(hash[:])
}
// Hash - Generate a hash value (message digest)
//
// Original: https://www.php.net/manual/en/function.hash.php
//
func Hash(cryp, val string) string {
switch cryp {
case "sha256":
return Sha256(val)
case "sha224":
return Sha224(val)
case "sha384":
return Sha384(val)
case "sha512":
return Sha512(val)
case "sha1":
return Sha1(val)
default:
return MD5(val)
}
}