-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (107 loc) · 2.93 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
package main
// #include <string.h>
// #include <stdbool.h>
// #include <mysql.h>
// #cgo CFLAGS: -O3 -I/usr/include/mysql -fno-omit-frame-pointer
import "C"
import (
"encoding/hex"
"hash"
"log"
"os"
"unsafe"
"golang.org/x/crypto/sha3"
)
// main function is needed even for generating shared object files
func main() {}
var l = log.New(os.Stderr, "sha3: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile)
func msg(message *C.char, s string) {
m := C.CString(s)
defer C.free(unsafe.Pointer(m))
C.strcpy(message, m)
}
//export Sha3_init
func Sha3_init(initid *C.UDF_INIT, args *C.UDF_ARGS, message *C.char) C.bool {
if args.arg_count != 2 {
msg(message, "`sha3`() requires 2 parameters: the message part, and the number of bits")
return C.bool(true)
}
argsTypes := (*[2]uint32)(unsafe.Pointer(args.arg_type))
argsTypes[0] = C.STRING_RESULT
argsTypes[1] = C.INT_RESULT
initid.maybe_null = C.bool(true)
return C.bool(false)
}
//export Sha3
func Sha3(initid *C.UDF_INIT, args *C.UDF_ARGS, result *C.char, length *uint64, isNull *C.char, message *C.char) *C.char {
*isNull = 1
argsArgs := (*[1 << 30]*C.char)(unsafe.Pointer(args.args))[:2:2]
bits := (*[1 << 30]*uint64)(unsafe.Pointer(args.args))[1]
if argsArgs[0] == nil || argsArgs[1] == nil {
return nil
}
var h hash.Hash
switch *bits {
case 224:
h = sha3.New224()
case 256:
h = sha3.New256()
case 384:
h = sha3.New384()
case 512:
h = sha3.New512()
default:
return nil
}
argsLengths := (*[1 << 30]uint64)(unsafe.Pointer(args.lengths))[:1:1]
s := C.GoStringN(argsArgs[0], C.int(argsLengths[0]))
b := make([]byte, *bits/8)
h.Write([]byte(s))
h.Sum(b[:0])
s = hex.EncodeToString(b)
*length = uint64(len(s))
*isNull = 0
return C.CString(s)
}
//export unhex_sha3_init
func unhex_sha3_init(initid *C.UDF_INIT, args *C.UDF_ARGS, message *C.char) C.bool {
if args.arg_count != 2 {
msg(message, "`unhex_sha3`() requires 2 parameters: the message part, and the number of bits")
return C.bool(true)
}
argsTypes := (*[2]uint32)(unsafe.Pointer(args.arg_type))
argsTypes[0] = C.STRING_RESULT
argsTypes[1] = C.INT_RESULT
initid.maybe_null = C.bool(true)
return C.bool(false)
}
//export unhex_sha3
func unhex_sha3(initid *C.UDF_INIT, args *C.UDF_ARGS, result *C.char, length *uint64, isNull *C.char, message *C.char) *C.char {
*isNull = 1
argsArgs := (*[1 << 30]*C.char)(unsafe.Pointer(args.args))[:2:2]
bits := (*[1 << 30]*uint64)(unsafe.Pointer(args.args))[1]
if argsArgs[0] == nil || argsArgs[1] == nil {
return nil
}
var h hash.Hash
switch *bits {
case 224:
h = sha3.New224()
case 256:
h = sha3.New256()
case 384:
h = sha3.New384()
case 512:
h = sha3.New512()
default:
return nil
}
argsLengths := (*[1 << 30]uint64)(unsafe.Pointer(args.lengths))[:1:1]
s := C.GoStringN(argsArgs[0], C.int(argsLengths[0]))
b := make([]byte, *bits/8)
h.Write([]byte(s))
h.Sum(b[:0])
*length = uint64(len(b))
*isNull = 0
return C.CString(string(b))
}