-
Notifications
You must be signed in to change notification settings - Fork 664
/
process.go
62 lines (52 loc) · 1.87 KB
/
process.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
package crypto
import (
"github.com/yaoapp/gou/process"
"github.com/yaoapp/kun/exception"
)
func init() {
process.Register("yao.crypto.hash", ProcessHash) // deprecated → crypto.Hash
process.Register("yao.crypto.hmac", ProcessHmac) // deprecated → crypto.Hash
process.Alias("yao.crypto.hash", "crypto.Hash")
process.Alias("yao.crypto.hmac", "crypto.Hmac")
}
// ProcessHash yao.crypto.hash Crypto Hash
// Args[0] string: the hash function name. MD4/MD5/SHA1/SHA224/SHA256/SHA384/SHA512/MD5SHA1/RIPEMD160/SHA3_224/SHA3_256/SHA3_384/SHA3_512/SHA512_224/SHA512_256/BLAKE2s_256/BLAKE2b_256/BLAKE2b_384/BLAKE2b_512
// Args[1] string: value
func ProcessHash(process *process.Process) interface{} {
process.ValidateArgNums(2)
typ := process.ArgsString(0)
value := process.ArgsString(1)
h, has := HashTypes[typ]
if !has {
exception.New("%s does not support", 400, typ).Throw()
}
res, err := Hash(h, value)
if err != nil {
exception.New("%s error: %s value: %s", 400, typ, err, value).Throw()
}
return res
}
// ProcessHmac yao.crypto.hmac Crypto the Keyed-Hash Message Authentication Code (HMAC) Hash
// Args[0] string: the hash function name. MD4/MD5/SHA1/SHA224/SHA256/SHA384/SHA512/MD5SHA1/RIPEMD160/SHA3_224/SHA3_256/SHA3_384/SHA3_512/SHA512_224/SHA512_256/BLAKE2s_256/BLAKE2b_256/BLAKE2b_384/BLAKE2b_512
// Args[1] string: value
// Args[2] string: key
// Args[3] string: base64
func ProcessHmac(process *process.Process) interface{} {
process.ValidateArgNums(3)
typ := process.ArgsString(0)
value := process.ArgsString(1)
key := process.ArgsString(2)
h, has := HashTypes[typ]
if !has {
exception.New("%s does not support", 400, typ).Throw()
}
encoding := ""
if process.NumOfArgs() > 3 {
encoding = process.ArgsString(3)
}
res, err := Hmac(h, value, key, encoding)
if err != nil {
exception.New("%s error: %s value: %s", 400, typ, err, value).Throw()
}
return res
}