Password generator: Difference between revisions

added RPL
No edit summary
(added RPL)
 
(5 intermediate revisions by 4 users not shown)
Line 2,098:
<syntaxhighlight lang="futurebasic">
output file "Password Generator"
 
include "Tlbx GameplayKit.incl"
 
Line 2,123 ⟶ 2,122:
_window = 1
begin enum output 1
_optionsLabel
_pwLengthLabel
_pwLengthField
_pwAmountLabel
_pwAmountField
_passwordScroll
_passwordView
_offscreenPrintView
_createPasswordsBtn
_optionsHelp
_checkOmitIs
Line 2,139 ⟶ 2,132:
_charactersLongLabel
_passwordLengthField
_pwAmountLabel
_pwAmountField
_seedLabel
_seedField
_saveBtn
_printBtn
_helpBtn
_bottomLine
_saveBtn
_printBtn
_createPasswordsBtn
end enum
 
Line 2,153 ⟶ 2,149:
 
_savePanel = 1
_helpAlert = 1
_pwLenAlert = 2
 
Line 2,176 ⟶ 2,171:
 
void local fn BuildWindow
long tag
CGRect r = fn CGRectMake( 0, 0, 672, 460 )
window _window, @"Rosetta Code Password Generator", r//, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable + NSWindowStyleMaskMiniaturizable
WindowSetContentMinSize( _window, fn CGSizeMake(310,386) )
r = fn CGRectMake( 20, 73, 515, 370 )
scrollview _passwordScroll, r, NSLineBorder,,_window
ViewSetAutoresizingMask( _passwordScroll, NSViewWidthSizable + NSViewHeightSizable )
textview _passwordView, r, _passwordScroll,, _window
Line 2,237 ⟶ 2,236:
ControlSetFormat( _seedField, @"0123456789", YES, 6, 0 )
ViewSetToolTip( _seedField, @"Enter optional random seed number." )
for tag = _optionsHelp to _seedField
ViewSetAutoresizingMask( tag, NSViewMinXMargin + NSViewMinYMargin )
next
r = fn CGRectMake( 631, 70, 25, 25 )
button _helpBtn, YES,,, r, NSButtonTypeMomentaryLight, NSBezelStyleHelpButton, _window
ViewSetToolTip( _helpBtn, @"Click for application help instructions." )
ViewSetAutoresizingMask( _helpBtn, NSViewMinXMargin + NSViewMaxYMargin )
r = fn CGRectMake( 20, 55, 632, 5 )
box _bottomline,, r, NSBoxSeparator
ViewSetAutoresizingMask( _bottomline, NSViewWidthSizable + NSViewMaxYMargin )
r = fn CGRectMake( 52220, 15, 13162, 32 )
button _createPasswordsBtn_printBtn, YES, , @"Create passwordsPrint…", r, NSButtonTypeMomentaryLight, NSBezelStyleRegularSquare, _window
ViewSetAutoresizingMask( _printBtn, NSViewMaxXMargin + NSViewMaxYMargin )
r = fn CGRectMake( 91, 15, 62, 32 )
button _saveBtn, YES, , @"Save…", r, NSButtonTypeMomentaryLight, NSBezelStyleRegularSquare, _window
ViewSetAutoresizingMask( _saveBtn, NSViewMaxXMargin + NSViewMaxYMargin )
r = fn CGRectMake( 20522, 15, 62131, 32 )
button _printBtn_createPasswordsBtn, YES, , @"Print…Create passwords", r, NSButtonTypeMomentaryLight, NSBezelStyleRegularSquare, _window
ViewSetAutoresizingMask( _createPasswordsBtn, NSViewMinXMargin + NSViewMaxYMargin )
r = fn CGRectMake( 631, 70, 25, 25 )
button _helpBtn, YES,,, r, NSButtonTypeMomentaryLight, NSBezelStyleHelpButton, _window
ViewSetToolTip( _helpBtn, @"Click for application help instructions." )
end fn
 
Line 2,400 ⟶ 2,408:
end fn
 
// Build character arrays at launch and save as application property
 
void local fn CreateCharactersDictionary
CFArrayRef charArray = @[¬
Line 2,460 ⟶ 2,468:
{{output}}
[[File:Password Generator.png]]
 
 
 
=={{header|Gambas}}==
Line 2,959 ⟶ 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,860 ⟶ 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,597 ⟶ 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
 
Line 5,761 ⟶ 5,899:
 
The generated passwords have been written to the file pwds.txt
</pre>
 
=={{header|XPL0}}==
{{trans|Python}}
<syntaxhighlight lang "XPL0">string 0;
 
func In(C, Str);
char C, Str;
[while Str(0) do
if C = Str(0) then return true else Str:= Str+1;
return false;
];
 
func New_password(Length, Readable);
int Length, Readable, I, C;
char Password_chars(100), Punctuation, Visually_similar;
[if Length < 4 then
[Text(0, "Password length = "); IntOut(0, Length);
Text(0, " is too short, minimum length = 4.");
return "";
];
Punctuation:= "!^"#$%&\'()*+,-./:;<=>?@[]^^_{|}~";
Visually_similar:= "Il1O05S2Z";
for I:= 0 to Length-1 do
[case Ran(4) of
0: C:= Ran(26) + ^a;
1: C:= Ran(26) + ^A;
2: C:= Ran(10) + ^0;
3: C:= Punctuation(Ran(31))
other [];
Password_chars(I):= C;
if Readable and In(C, Visually_similar) then I:= I-1;
];
Password_chars(I):= 0;
return Password_chars;
];
 
proc Password_generator(Length, Qty, Readable);
int Length, Qty, Readable, I;
for I:= 0 to Qty-1 do
[Text(0, New_password(Length, Readable)); CrLf(0)];
 
[Password_generator(14, 4, true);
Password_generator( 8, 4, false);
]</syntaxhighlight>
{{out}}
<pre>
GojA9ep6=3|U,\
U9@p|f'WH7+&TM
8=E7xvm6J9Y([q
zrknig;8Pv+(Va
37s11T68
Ir)wY0P<
54d3o]Ga
95??s]"O
</pre>
 
1,150

edits