]> gitweb.factorcode.org Git - factor.git/commitdiff
random.passwords: new vocab
authorAlexander Iljin <ajsoft@yandex.ru>
Thu, 14 Nov 2019 18:03:13 +0000 (19:03 +0100)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 15 Nov 2019 17:58:32 +0000 (09:58 -0800)
basis/random/passwords/authors.txt [new file with mode: 0644]
basis/random/passwords/passwords-docs.factor [new file with mode: 0644]
basis/random/passwords/passwords-tests.factor [new file with mode: 0644]
basis/random/passwords/passwords.factor [new file with mode: 0644]

diff --git a/basis/random/passwords/authors.txt b/basis/random/passwords/authors.txt
new file mode 100644 (file)
index 0000000..8e1955f
--- /dev/null
@@ -0,0 +1 @@
+Alexander Ilin
diff --git a/basis/random/passwords/passwords-docs.factor b/basis/random/passwords/passwords-docs.factor
new file mode 100644 (file)
index 0000000..7f9d9df
--- /dev/null
@@ -0,0 +1,48 @@
+! Copyright (C) 2019 Alexander Ilin.
+! See http://factorcode.org/license.txt for BSD license.
+USING: help.markup help.syntax kernel random strings ;
+IN: random.passwords
+
+ABOUT: "random.passwords"
+
+ARTICLE: "random.passwords" "Generating random passwords"
+"The " { $vocab-link "random.passwords" } " vocab provides functions for generation of random passwords."
+$nl
+"Generate password of a given length from some often used character sets:"
+{ $subsections alnum-password hex-password ascii-password }
+"Generate a password from a custom character set:"
+{ $subsections password }
+;
+
+HELP: password
+{ $values
+    { "n" "password length" }
+    { "charset" string }
+    { "string" string }
+}
+{ $description "Generate a password of length " { $snippet "n" } " by randomly selecting characters from the " { $snippet "charset" } " string. All characters of the " { $snippet "charset" } " have equal probability of appearing at any position of the result."
+$nl
+"If " { $snippet "n" } " = 0, return empty string. If " { $snippet "n" } " < 0, throw an error."
+$nl
+{ $link secure-random-generator } " is used as the randomness source." } ;
+
+HELP: alnum-password
+{ $values
+    { "n" "password length" }
+    { "string" string }
+}
+{ $description "Generate a random password consisting of " { $snippet "n" } " alphanumeric characters (0..9, A..Z, a..z)." } ;
+
+HELP: ascii-password
+{ $values
+    { "n" "password length" }
+    { "string" string }
+}
+{ $description "Generate a random password consisting of " { $snippet "n" } " printable ASCII characters." } ;
+
+HELP: hex-password
+{ $values
+    { "n" "password length" }
+    { "string" string }
+}
+{ $description "Generate a random password consisting of " { $snippet "n" } " hexadecimal characters (0..9, A..F)." } ;
diff --git a/basis/random/passwords/passwords-tests.factor b/basis/random/passwords/passwords-tests.factor
new file mode 100644 (file)
index 0000000..b47e682
--- /dev/null
@@ -0,0 +1,14 @@
+! Copyright (C) 2019 Alexander Ilin.
+! See http://factorcode.org/license.txt for BSD license.
+USING: math math.parser random.passwords sequences tools.test ;
+IN: random.passwords.tests
+
+{ "aaaaaaaaaa" } [ 10 "a" password ] unit-test
+{ 10 } [ 10 "ab" password length ] unit-test
+{ "" } [ 0 "ab" password ] unit-test
+[ -1 "ab" password ] must-fail
+
+{ 2 } [ 2 ascii-password length ] unit-test
+{ 3 } [ 3 alnum-password length ] unit-test
+{ 4 } [ 4 hex-password length ] unit-test
+{ t } [ 4 hex-password hex> 65535 <= ] unit-test
diff --git a/basis/random/passwords/passwords.factor b/basis/random/passwords/passwords.factor
new file mode 100644 (file)
index 0000000..44cd127
--- /dev/null
@@ -0,0 +1,27 @@
+! Copyright (C) 2019 Alexander Ilin.
+! See http://factorcode.org/license.txt for BSD license.
+USING: fry literals math.ranges random sequences ;
+IN: random.passwords
+
+<PRIVATE
+
+CONSTANT: ascii-printable-charset $[ 33 126 [a,b] ]
+CONSTANT: hex-charset "0123456789ABCDEF"
+CONSTANT: alphanum-charset $[
+    CHAR: 0 CHAR: 9 [a,b]
+    CHAR: a CHAR: z [a,b] append
+    CHAR: A CHAR: Z [a,b] append ]
+
+PRIVATE>
+
+: password ( n charset -- string )
+    '[ [ _ random ] "" replicate-as ] with-secure-random ;
+
+: ascii-password ( n -- string )
+    ascii-printable-charset password ;
+
+: hex-password ( n -- string )
+    hex-charset password ;
+
+: alnum-password ( n -- string )
+    alphanum-charset password ;