File: Bcrypt.xs

package info (click to toggle)
libcrypt-bcrypt-perl 0.011-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 160 kB
  • sloc: ansic: 696; perl: 78; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,165 bytes parent folder | download
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
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "crypt_blowfish.h"

#define BCRYPT_HASHSIZE 64

static int timing_safe_compare(const unsigned char *str1, const unsigned char *str2, STRLEN length) {
	int ret = 0;
	int i;

	for (i = 0; i < length; ++i)
		ret |= (str1[i] ^ str2[i]);

	return ret == 0;
}

MODULE = Crypt::Bcrypt              PACKAGE = Crypt::Bcrypt

PROTOTYPES: DISABLE

const char* _bcrypt_hashpw(const char* password, const char* settings)
CODE:
	char outhash[BCRYPT_HASHSIZE];
	const char* output = _crypt_blowfish_rn(password, settings, outhash, BCRYPT_HASHSIZE);
	if (output == NULL)
		Perl_croak(aTHX_ "Could not hash: %s", strerror(errno));
	RETVAL = outhash;
OUTPUT:
	RETVAL

int bcrypt_check(const char* password, const char* hash, STRLEN length(hash))
CODE:
	char outhash[BCRYPT_HASHSIZE];
	STRLEN hashlen;
	const char* ret = _crypt_blowfish_rn(password, hash, outhash, BCRYPT_HASHSIZE);
	if (!ret || strlen(outhash) != STRLEN_length_of_hash)
		RETVAL = 0;
	else
		RETVAL = timing_safe_compare((const unsigned char *)hash, (const unsigned char *)outhash, STRLEN_length_of_hash);
OUTPUT:
	RETVAL