Password generator: Difference between revisions

added RPL
(Added XPL0 example.)
(added RPL)
 
(3 intermediate revisions by 2 users not shown)
Line 2,965:
</pre>
 
 
=={{header|jq}}==
{{Works with|jq}}
 
'''Works with gojq, the Go implementation of jq'''
 
'''Adapted from [[#Julia|Julia]]'''
 
The following assumes that an external source of randomness such as /dev/urandom is available
and that jq is invoked along the lines of the following:
<pre>
< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -MRnr -f password-generator.jq
</pre>
<syntaxhighlight lang="jq">
# Output: a prn in range(0;$n) where $n is `.`
def prn:
if . == 1 then 0
else . as $n
| ([1, (($n-1)|tostring|length)]|max) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
# Input: an array
# Output: an array, being a selection of $k elements from . chosen with replacement
def prns($k):
. as $in
| length as $n
| [range(0; $k) | $in[$n|prn]];
 
def knuthShuffle:
length as $n
| if $n <= 1 then .
else {i: $n, a: .}
| until(.i == 0;
.i += -1
| (.i + 1 | prn) as $j
| .a[.i] as $t
| .a[.i] = .a[$j]
| .a[$j] = $t)
| .a
end;
 
# Generate a single password of length $len >= 4;
# certain confusable characters are only allowed iff $simchars is truthy.
def passgen($len; $simchars):
def stoa: explode | map([.]|implode);
if $len < 4 then "length must be at least 4" | error
else
{ DIGIT: ("0123456789" | stoa),
UPPER: [range(65;91) | [.] | implode], # A-Z
LOWER: [range(97;123) | [.] | implode], # a-z
OTHER: ("!\"#$%&'()*+,-./:;<=>?@[]^_{|}~" | stoa) }
| if $simchars|not
then .DIGIT |= . - ["0", "1", "2", "5"]
| .UPPER |= . - ["O", "I", "Z", "S"]
| .LOWER |= . - ["l"]
end
| (reduce (.DIGIT, .UPPER, .LOWER, .OTHER) as $set ([];
. + ($set | prns(1)))) +
(.DIGIT + .UPPER + .LOWER + .OTHER | prns($len - 4))
| knuthShuffle
| join("")
end ;
 
def passgen($len):
passgen($len; true);
 
# Generate a stream of $npass passwords, each of length $len;
# certain confusable characters are only allowed iff $simchars is truthy.
def passgen($len; $npass; $seed; $simchars):
if ($seed | type) == "number" and $seed > 0 then $seed | prn else null end
| range(0; $npass)
| passgen($len; $simchars) ;
 
 
### Examples:
"Without restriction:", passgen(12; 5; null; true),
"",
"Certain confusable characters are disallowed:", passgen(12; 5; null; false)
</syntaxhighlight>
{{output}}
<pre>
Without restriction:
yT8+9t7=wfEn
#]j<HIpThJ6A
|~O{psk*[5)I
w_(N%QrI5:5#
$o3sfdKv"'9~
 
Certain confusable characters are disallowed:
>oXgj>H9wX^[
a^JR+v!bk6GU
7$~yp?YG]G|E
!/_MM_g.p{a4
iD?+xU+!A4]R
</pre>
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 4,866 ⟶ 4,964:
password = u8/Ah8%H
password = c4\Nc2_J
</pre>
 
=={{header|RPL}}==
{{works with|HP|48G}}
« "!" 34 CHR + "#$%&'()*+,-./:;<=>?@[]^_{|}~" + "" → chars pwd
« { 0 0 0 0 }
'''WHILE''' DUP2 ∑LIST 4 ≠ OR '''REPEAT'''
RAND 4 * CEIL
{ « RAND 25 * FLOOR 65 + CHR »
« RAND 25 * FLOOR 97 + CHR »
« RAND 10 * FLOOR →STR »
« chars RAND OVER SIZE * CEIL DUP SUB » }
OVER GET EVAL 'pwd' STO+
1 PUT
SWAP 1 - 0 MAX SWAP
'''END''' DROP2 pwd
» » '<span style="color:blue">→PWD</span>' STO ''<span style="color:grey">@ ( length → "password" )''</span>
« → length n
« '''IF''' length 4 < '''THEN'''
"Length must be at least 4" DOERR
'''ELSE'''
{ }
1 n '''FOR''' j
'''WHILE''' length <span style="color:blue">→PWD</span> DUP SIZE length > '''REPEAT''' DROP '''END'''
+
'''NEXT'''
'''END'''
» » '<span style="color:blue">PWDS</span>' STO <span style="color:grey">''@ ( length n → { "password1" .. "passwordn" } )''</span>
 
8 3 <span style="color:blue">PWDS</span>
{{out}}
<pre>
1: { "v7-c8d.B" "oVe1M$17" "R+I6vJ9j" }
</pre>
 
Line 5,603 ⟶ 5,735:
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="pythonwren">import "random" for Random
import "./ioutil" for FileUtil, File, Input
import "./fmt" for Fmt
import "os" for Process
 
1,151

edits