Skip to content

Commit

Permalink
random.passwords: new vocab
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ilin authored and mrjbq7 committed Nov 15, 2019
1 parent cee0229 commit 71ad025
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions basis/random/passwords/authors.txt
@@ -0,0 +1 @@
Alexander Ilin
48 changes: 48 additions & 0 deletions 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)." } ;
14 changes: 14 additions & 0 deletions 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
27 changes: 27 additions & 0 deletions 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

<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 ;

0 comments on commit 71ad025

Please sign in to comment.