-
Notifications
You must be signed in to change notification settings - Fork 211
/
proquint.factor
63 lines (48 loc) · 1.3 KB
/
proquint.factor
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
! Copyright (C) 2023 John Benediktsson
! See https://factorcode.org/license.txt for BSD license
USING: base64.private ip-parser kernel literals make math
math.bitwise random sequences ;
IN: proquint
! https://arxiv.org/html/0901.4016
<PRIVATE
<<
CONSTANT: consonant "bdfghjklmnprstvz"
CONSTANT: vowel "aiou"
>>
: >quint16 ( m -- str )
5 [
even? [
[ -4 shift ] [ 4 bits consonant nth ] bi
] [
[ -2 shift ] [ 2 bits vowel nth ] bi
] if
] "" map-integers-as reverse nip ;
PRIVATE>
: >quint ( m bits -- str )
[
[ dup 0 > ] [
[ [ 16 bits >quint16 , ] [ -16 shift ] bi ] dip 16 -
] while 2drop
] { } make reverse "-" join ;
: quint> ( str -- m )
0 [
dup $[ consonant alphabet-inverse ] nth [
nip [ 4 shift ] [ + ] bi*
] [
dup $[ vowel alphabet-inverse ] nth [
nip [ 2 shift ] [ + ] bi*
] [
CHAR: - assert=
] if*
] if*
] reduce ;
: quint-password ( bits -- quint )
[ random-bits ] [ >quint ] bi ;
: ipv4>quint ( ipv4 -- str )
ipv4-aton 32 >quint ;
: quint>ipv4 ( str -- ipv4 )
quint> ipv4-ntoa ;
: ipv6>quint ( ipv6 -- str )
ipv6-aton 128 >quint ;
: quint>ipv6 ( str -- ipv6 )
quint> ipv6-ntoa ;