From d8c4fd9aef9137b04e4311a1f50024ab88d4c6e3 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 17 Jun 2016 00:29:13 -0400 Subject: Add some _XOPEN_CRYPT stuff. --- crypt/crypt.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 crypt/crypt.go diff --git a/crypt/crypt.go b/crypt/crypt.go new file mode 100644 index 0000000..40b5149 --- /dev/null +++ b/crypt/crypt.go @@ -0,0 +1,66 @@ +// Copyright 2015-2016 Luke Shumaker . +// +// This is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 2.1 of the +// License, or (at your option) any later version. +// +// This software is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this manual; if not, see +// . + +// Package crypt provides an interface to the POSIX CRYPT option +// group. +// +// Actually, it doesn't yet support encrypt() or setkey() +package crypt + +import "unsafe" + +/* +#cgo LDFLAGS: -lcrypt +#define _GNU_SOURCE // for crypt_r(3) in crypt.h +#include // for free(3) +#include // for crypt_r(3) +#include // for strdup(3) and memset(3) +char *c_crypt(const char *key, const char *salt) +{ + struct crypt_data data; + data.initialized = 0; + char *hash = crypt_r(key, salt, &data); + if (hash) + hash = strdup(hash); + memset(&data, 0, sizeof(data)); + return hash; +} +*/ +import "C" + +func Crypt(key string, salt string) string { + ckey := C.CString(key) + defer C.free(unsafe.Pointer(ckey)) + csalt := C.CString(salt) + defer C.free(unsafe.Pointer(csalt)) + chash := C.c_crypt(ckey, csalt) + defer C.free(unsafe.Pointer(chash)) + hash := C.GoString(chash) + return hash +} + +func SaltOk(salt string) bool { + if len(salt) < 2 { + return false + } + hash := Crypt("", salt) + if len(hash) < 2 { + return false + } + return salt[0] == hash[0] && salt[1] == hash[1] +} + +const SaltAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./" -- cgit v1.1-4-g5e80