-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassword.go
40 lines (33 loc) · 979 Bytes
/
password.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
package main
// #include "redismodule.h"
import "C"
import (
"github.com/amyangfei/RedisModules-Go/cgoutils"
"golang.org/x/crypto/bcrypt"
"unsafe"
)
//export GoDoCrypt
func GoDoCrypt(password *C.char, length C.int) (*C.char, int, *C.char, int) {
passwdSlice := cgoutils.ZeroCopySlice(
unsafe.Pointer(password), int(length), int(length), false)
// Hashing the password with cost of 5
hashed, err := bcrypt.GenerateFromPassword(passwdSlice.Data, 5)
if err != nil {
errstr := err.Error()
return C.CString(""), 0, C.CString(errstr), len(errstr)
} else {
return C.CString(string(hashed)), len(hashed), C.CString(""), 0
}
}
//export GoDoValidate
func GoDoValidate(hash, password *C.char, hashLen, passLen C.int) int {
hashSlice := C.GoBytes(unsafe.Pointer(hash), hashLen)
passSlice := C.GoBytes(unsafe.Pointer(password), passLen)
err := bcrypt.CompareHashAndPassword(hashSlice, passSlice)
if err != nil {
return 0
} else {
return 1
}
}
func main() {}