From 71ad025aaf2b888119d4ac080cf5ac4c8c3a0b52 Mon Sep 17 00:00:00 2001 From: Alexander Iljin Date: Thu, 14 Nov 2019 19:03:13 +0100 Subject: [PATCH] random.passwords: new vocab --- basis/random/passwords/authors.txt | 1 + basis/random/passwords/passwords-docs.factor | 48 +++++++++++++++++++ basis/random/passwords/passwords-tests.factor | 14 ++++++ basis/random/passwords/passwords.factor | 27 +++++++++++ 4 files changed, 90 insertions(+) create mode 100644 basis/random/passwords/authors.txt create mode 100644 basis/random/passwords/passwords-docs.factor create mode 100644 basis/random/passwords/passwords-tests.factor create mode 100644 basis/random/passwords/passwords.factor diff --git a/basis/random/passwords/authors.txt b/basis/random/passwords/authors.txt new file mode 100644 index 00000000000..8e1955f8e15 --- /dev/null +++ b/basis/random/passwords/authors.txt @@ -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 index 00000000000..7f9d9dfc340 --- /dev/null +++ b/basis/random/passwords/passwords-docs.factor @@ -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 index 00000000000..b47e682c37f --- /dev/null +++ b/basis/random/passwords/passwords-tests.factor @@ -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 index 00000000000..44cd127efdc --- /dev/null +++ b/basis/random/passwords/passwords.factor @@ -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 + + + +: 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 ;