This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcrypt.l
81 lines (61 loc) · 1.86 KB
/
bcrypt.l
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# bcrypt.l
#
# The MIT License (MIT)
#
# Copyright (c) 2015-2020 Alexander Williams, Unscramble <license@unscramble.jp>
(load (pack (car (file)) "module.l"))
(setq
*Bcrypt (pack (car (file)) ".lib/libbcrypt.so")
*BCRYPT_HASHSIZE 64
*Work_factor 12 )
(setq *Salt_struct '(`*BCRYPT_HASHSIZE B . `*BCRYPT_HASHSIZE))
# ffi-bindings
(de bcrypt-gensalt (Factor &salt)
(use Salt
(cons
(native `*Bcrypt "bcrypt_gensalt" 'I Factor (cons 'Salt &salt 0))
Salt ]
(de bcrypt-hashpw (Passwd Salt &hash)
(use Hash
(cons
(native `*Bcrypt "bcrypt_hashpw" 'I Passwd Salt (cons 'Hash &hash 0))
Hash ]
(de bcrypt-checkpw (Passwd Hash)
(native `*Bcrypt "bcrypt_checkpw" 'I Passwd Hash) )
# internal
(de throw-error (Message)
(throw 'InternalError (cons 'BcryptError Message)) )
[de epoch ()
(let Epoch (native "@" "time" 'I 0)
(if (ge0 Epoch)
@
(throw-error "Unable to obtain UNIX Epoch") ]
[de to-string (Hash)
(pack (mapcar char (cdr Hash) ]
# public
[de gensalt (Factor)
(default Factor *Work_factor)
(let Result (bcrypt-gensalt Factor *Salt_struct)
(if (n0 (car Result))
(throw-error "Unable to generate salt")
(to-string Result) ]
[de hashpw (Passwd Salt)
(default Salt *Work_factor)
(let Hash (if (num? Salt)
(gensalt Salt)
Salt )
(let Result (bcrypt-hashpw Passwd Hash *Salt_struct)
(if (n0 (car Result))
(throw-error "Unable to hash password")
(to-string Result) ]
[de compare (Passwd Hash)
(let Matched (bcrypt-checkpw Passwd Hash)
(case Matched
(-1 (throw-error "Unable to hash password"))
(0 T) ]
[de timing (Factor)
(default Factor *Work_factor)
(let Start (epoch)
(hashpw "speed test to hash a password" Factor)
(let End (epoch)
(cons Factor (- End Start)) ]