Ejemplo n.º 1
0
// Crypt generates passwords using crypt(3)
// crypt wraps C library crypt_r
func Crypt(key, salt string) string {
	data := C.struct_crypt_data{}
	ckey := C.CString(key)
	csalt := C.CString(salt)
	out := C.GoString(C.crypt_r(ckey, csalt, &data))
	C.free(unsafe.Pointer(ckey))
	C.free(unsafe.Pointer(csalt))
	return out
}
Ejemplo n.º 2
0
// Crypt encrypts a plain text password with given salt.
func Crypt(plain string, salt BcryptSalt) (hashed string, err error) {
	cpass := C.CString(plain)
	defer C.free(unsafe.Pointer(cpass))
	csalt := C.CString(string(salt))
	defer C.free(unsafe.Pointer(csalt))
	data := C.malloc(C.CRYPT_OUTPUT_SIZE)
	defer C.free(data)

	out, err := C.crypt_r(cpass, csalt, data)
	if err != nil {
		return
	}

	hashed = C.GoString(out)
	return
}