-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.ur
40 lines (31 loc) · 1.33 KB
/
crypto.ur
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
open Process
type hashed = {Hash : string, Salt : string}
val getHash = fn x => x.Hash
val getSalt = fn x => x.Salt
fun random (length : int) : transaction string =
output <- Process.exec ("head -c " ^ show length ^ " /dev/urandom | base64") (textBlob "") (10 * length);
if
status output = 0
then
return (String.trim (Process.blobText (Process.blob output)))
else
return (error <xml>ERROR: Function 'random' generated non-zero exit code.</xml>)
fun constructHash (password : string) (salt : string) : transaction hashed =
hasherResult <- Process.exec ("sha256sum | awk '{print $1}'") (textBlob (password ^ salt)) 100;
if
status hasherResult = 0
then
return {Hash = (String.trim (blobText (blob hasherResult))), Salt = salt}
else
return (error <xml>ERROR: Function 'constructHash' generated non-zero exit code.</xml>)
fun hashWithSalt (password : string) (saltSize : int) : transaction hashed =
salt <- random saltSize;
constructHash password salt
fun hash (password : string) : transaction hashed = hashWithSalt password 70
fun verify (password : string) (correctHash : string) (salt : string): transaction bool =
guess <- constructHash password salt;
return (correctHash = (getHash guess))
fun token (length : int) =
out <- random length;
hashOut <- hash out;
return {Token = out, Hash = hashOut}