|
Packit |
63bb0d |
// +build !darwin
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Copied from https://github.com/amoghe/go-crypt/blob/b3e291286513a0c993f7c4dd7060d327d2d56143/crypt_r.go
|
|
Packit |
63bb0d |
// Original sources are under MIT license:
|
|
Packit |
63bb0d |
// The MIT License (MIT)
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// Copyright (c) 2015 Akshay Moghe
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit |
63bb0d |
// of this software and associated documentation files (the "Software"), to deal
|
|
Packit |
63bb0d |
// in the Software without restriction, including without limitation the rights
|
|
Packit |
63bb0d |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
Packit |
63bb0d |
// copies of the Software, and to permit persons to whom the Software is
|
|
Packit |
63bb0d |
// furnished to do so, subject to the following conditions:
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// The above copyright notice and this permission notice shall be included in all
|
|
Packit |
63bb0d |
// copies or substantial portions of the Software.
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit |
63bb0d |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit |
63bb0d |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
Packit |
63bb0d |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
Packit |
63bb0d |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
Packit |
63bb0d |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
Packit |
63bb0d |
// SOFTWARE.
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// Package crypt provides wrappers around functions available in crypt.h
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// It wraps around the GNU specific extension (crypt_r) when it is available
|
|
Packit |
63bb0d |
// (i.e. where GOOS=linux). This makes the go function reentrant (and thus
|
|
Packit |
63bb0d |
// callable from concurrent goroutines).
|
|
Packit |
63bb0d |
package crypt
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
import (
|
|
Packit |
63bb0d |
"unsafe"
|
|
Packit |
63bb0d |
)
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
/*
|
|
Packit |
63bb0d |
#cgo LDFLAGS: -lcrypt
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// this is needed for Ubuntu
|
|
Packit |
63bb0d |
#define _GNU_SOURCE
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
#include <stdlib.h>
|
|
Packit |
63bb0d |
#include <string.h>
|
|
Packit |
63bb0d |
#include <crypt.h>
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
char *gnu_ext_crypt(char *pass, char *salt) {
|
|
Packit |
63bb0d |
char *enc = NULL;
|
|
Packit |
63bb0d |
char *ret = NULL;
|
|
Packit |
63bb0d |
struct crypt_data data;
|
|
Packit |
63bb0d |
data.initialized = 0;
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
enc = crypt_r(pass, salt, &data);
|
|
Packit |
63bb0d |
if(enc == NULL) {
|
|
Packit |
63bb0d |
return NULL;
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
ret = (char *)malloc((strlen(enc)+1) * sizeof(char)); // for trailing null
|
|
Packit |
63bb0d |
strcpy(ret, enc);
|
|
Packit |
63bb0d |
ret[strlen(enc)]= '\0';
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
return ret;
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
*/
|
|
Packit |
63bb0d |
import "C"
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Crypt provides a wrapper around the glibc crypt_r() function.
|
|
Packit |
63bb0d |
// For the meaning of the arguments, refer to the package README.
|
|
Packit |
63bb0d |
func crypt(pass, salt string) (string, error) {
|
|
Packit |
63bb0d |
c_pass := C.CString(pass)
|
|
Packit |
63bb0d |
defer C.free(unsafe.Pointer(c_pass))
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
c_salt := C.CString(salt)
|
|
Packit |
63bb0d |
defer C.free(unsafe.Pointer(c_salt))
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
c_enc, err := C.gnu_ext_crypt(c_pass, c_salt)
|
|
Packit |
63bb0d |
if c_enc == nil {
|
|
Packit |
63bb0d |
return "", err
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
defer C.free(unsafe.Pointer(c_enc))
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Return nil error if the string is non-nil.
|
|
Packit |
63bb0d |
// As per the errno.h manpage, functions are allowed to set errno
|
|
Packit |
63bb0d |
// on success. Caller should ignore errno on success.
|
|
Packit |
63bb0d |
return C.GoString(c_enc), nil
|
|
Packit |
63bb0d |
}
|