random.passwords: new vocab
							parent
							
								
									cee0229b20
								
							
						
					
					
						commit
						71ad025aaf
					
				| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
Alexander Ilin
 | 
			
		||||
| 
						 | 
				
			
			@ -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)." } ;
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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 ;
 | 
			
		||||
		Loading…
	
		Reference in New Issue