-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.groovy
55 lines (45 loc) · 1.36 KB
/
Utils.groovy
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
import java.security.MessageDigest
class Utils {
static HEX_DIGITS = "0123456789abcdef"
static encodeAsHex(def theTarget) {
if (theTarget == null) {
return null
}
else {
def result = new StringBuffer()
theTarget.each() {
result << HEX_DIGITS[(it & 0xF0) >> 4]
result << HEX_DIGITS[(it & 0x0F)]
}
return result.toString()
}
}
static encodeAsMD5Hex(def theTarget) {
encodeAsHex(encodeAsMD5(theTarget))
}
static encodeAsMD5(def theTarget){
if (theTarget == null) {
return null
}
else {
def md = MessageDigest.getInstance("MD5")
return md.digest(theTarget.toString().getBytes())
}
}
static encodeAsSHA1Hex(def theTarget) {
encodeAsHex(encodeAsSHA1(theTarget))
}
static encodeAsSHA1(def theTarget){
if (theTarget == null) {
return null
}
else {
def md = MessageDigest.getInstance("SHA1")
return md.digest(theTarget.toString().getBytes())
}
}
static void mkdirs4file(filePath) {
int idx = filePath.lastIndexOf('/')
new File(filePath.substring(0,idx)).mkdirs()
}
}