Password generator: Difference between revisions
m
syntax highlighting fixup automation
Puppydrum64 (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 36:
=={{header|6502 Assembly}}==
Unfortunately, easy6502 cannot display text, but it does have a random number generator, so this example merely contains the logic for generating the password itself. The screen can only display colored pixels, and the top 4 bytes are masked off, so this is just a visual aid to show the process in action. The X register is loaded with the password's length (in this case, 20 characters, not counting the null terminator.)
<
LDX #20
LOOP:
Line 52:
DEX
BNE LOOP
BRK ;end program</
{{out}}
Line 67:
Help is printed when no arguments are given.
<
bits 16
;;; MS-DOS syscalls
Line 340:
lmask: resb 1 ; Mask for random number generation
rnddat: resb 4 ; RNG state
buffer: resb 257 ; Space to store password</
=={{header|Action!}}==
<
BYTE res
Line 467:
UNTIL again=0
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Password_generator.png Screenshot from Atari 8-bit computer]
Line 495:
=={{header|Ada}}==
<
with Ada.Strings.Fixed;
with Ada.Command_Line;
Line 686:
end loop;
end Mkpw;</
{{out}}
<pre>$ ./mkpw count 8 length 22 safe
Line 699:
=={{header|Applesoft BASIC}}==
<
2 L=8
3 M$=CHR$(13)
Line 857:
900 Q = 1
910 VTAB 23
920 RETURN</
=={{header|Arturo}}==
<
uppercase: `A`..`Z`
digits: `0`..`9`
Line 894:
loop 1..10 'x [
print generate to :integer first arg
]</
{{out}}
Line 912:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PASSWORD_GENERATOR.AWK [-v mask=x] [-v xt=x]
#
Line 1,010:
printf("example: %s -v mask=%s -v xt=%s\n",cmd,mask,xt)
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,022:
=={{header|C}}==
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
Line 1,149:
return 0;
}
</syntaxhighlight>
{{out}}
Line 1,163:
=={{header|C sharp|C#}}==
<
using System.Linq;
Line 1,237:
return new string(password);
}
}</
{{out}}
<pre>PASSGEN -l:12 -c:6
Line 1,248:
=={{header|C++}}==
<
#include <string>
#include <algorithm>
Line 1,302:
}
return 0;
}</
{{out}}
<pre>
Line 1,327:
<b>module.ceylon</b>:
<
module rosetta.passwordgenerator "1.0.0" {
import ceylon.random "1.2.2";
}
</syntaxhighlight>
<b>run.ceylon:</b>
<
import ceylon.random {
DefaultRandom,
Line 1,469:
Character[] filterCharsToExclude(Character[] chars, Character[] charsToExclude)
=> chars.filter((char) => ! charsToExclude.contains(char)).sequence();
</syntaxhighlight>
Line 1,509:
=={{header|Clojure}}==
<
(:require [clojure.set :refer [difference]]
[clojure.tools.cli :refer [parse-opts]])
Line 1,547:
(if (:help options) (println summary)
(dotimes [n (:count options)]
(println (apply str (generate-password options)))))))</
{{out}}
Line 1,571:
Note that on line 220, the <code>CLR</code> statement is called prior to restarting the program to avoid a <code>?REDIM'D ARRAY ERROR</code> if line 85 should execute. By default, single dimension arrays do not need to be DIMensioned if kept to 11 elements (0 through 10) or less. Line 85 checks for this.
<
2 rem rosetta code
10 g$(1)="abcdefghijklmnopqrstuvwxyz"
Line 1,633:
610 print "Press any key to begin."
615 get k$:if k$="" then 615
620 return</
{{out}}
Line 1,689:
=={{header|Common Lisp}}==
<
(defvar *lowercase* '(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k #\l #\m
#\n #\o #\p #\q #\r #\s #\t #\u #\v #\w #\x #\y #\z))
Line 1,736:
(loop for x from 1 to count do
(print (generate-password len human-readable)))))
</syntaxhighlight>
{{out}}
<pre>
Line 1,749:
=={{header|Crystal}}==
<
special_chars = true
Line 1,820:
break if count <= 0
end
</syntaxhighlight>
{{out}}
Line 1,867:
=={{header|Elixir}}==
<
@lower Enum.map(?a..?z, &to_string([&1]))
@upper Enum.map(?A..?Z, &to_string([&1]))
Line 1,905:
end
Password.generator</
{{out}}
Line 1,922:
=={{header|F_Sharp|F#}}==
===The function===
<
// A function to generate passwords of a given length. Nigel Galloway: May 2nd., 2018
let N = (set)"qwertyuiopasdfghjklzxcvbnm"
Line 1,933:
let fN n = not (Set.isEmpty (Set.intersect N n )||Set.isEmpty (Set.intersect I n )||Set.isEmpty (Set.intersect G n )||Set.isEmpty (Set.intersect E n ))
Seq.initInfinite(fun _->(set)(List.init n (fun _->L.[y.Next()%(Array.length L)])))|>Seq.filter fN|>Seq.map(Set.toArray >> System.String)
</syntaxhighlight>
===A possible use===
Print 5 password of length 8
<
pWords 8 |> Seq.take 5 |> Seq.iter(printfn "%s")
</syntaxhighlight>
{{out}}
<pre>
Line 1,950:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<
kernel math math.parser multiline namespaces peg.ebnf
prettyprint random sequences ;
Line 2,019:
[ parse-args gen-pwds ] [ 2drop usage print ] recover ;
MAIN: main</
{{out}}
<pre>
Line 2,044:
=={{header|FreeBASIC}}==
{{trans|Run BASIC}}<
charS(1) = "0123456789"
charS(2) = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Line 2,081:
Wend
Sleep
</syntaxhighlight>
{{out}}
<pre>------ Password Generator ------
Line 2,096:
=={{header|Gambas}}==
'''[https:c/gambas-playground.proko.eu/?gist=0ef1242c761d8a39297fb913fc6a56c0 Click this link to run this code]'''
<
' INSTRUCTIONS
Line 2,165:
Print sPassword 'Print the password list
End</
Output:
<pre>
Line 2,185:
=={{header|Go}}==
<syntaxhighlight lang="go">
package main
Line 2,278:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,296:
The function <code>password</code> for given length and a list of char sets which should be included, generates random password.
<
import Control.Monad.Random
import Data.List
Line 2,315:
x <- uniform lst
xs <- shuffle (delete x lst)
return (x : xs)</
For example:
Line 2,327:
User interface (uses a powerful and easy-to-use command-line [https://hackage.haskell.org/package/options-1.2.1.1/docs/Options.html option parser]).
<
data Opts = Opts { optLength :: Int
Line 2,353:
, "!\"#$%&'()*+,-./:;<=>?@[]^_{|}~" ]
visualySimilar = ["l","IOSZ","012","!|.,"]</
{{Out}}
Line 2,406:
Implementation:
<
chr=: a.&i.
Line 2,428:
y must be at least 4 because
passwords must contain four different kinds of characters.
)</
Example use (from J command line):
<
[x] pwgen y - generates passwords of length y
optional x says how many to generate (if you want more than 1)
Line 2,446:
Oo?|2oc4yi
9V9[EJ:Txs
$vYd(>4L:m</
=={{header|Java}}==
{{works with|Java|7}}
<
public class PasswordGenerator {
Line 2,515:
return sb.toString();
}
}</
<pre>7V;m
Line 2,529:
=={{header|JavaScript}}==
<
return this.split('').sort(() => Math.random() - .5).join('');
}
Line 2,582:
console.log( createPwd( {len: 20, num: 2}) );
console.log( createPwd( {len: 20, num: 2, noSims: false}) );
</syntaxhighlight>
{{out}}<pre>
> Q^g"7
Line 2,593:
{{works with|Julia|0.6}}
<
if len < 4; error("length must be at least 4") end
# Definitions
Line 2,621:
end
passgen(stdout, 10, 12; seed = 1)</
{{out}}
Line 2,638:
=={{header|Kotlin}}==
<
import java.util.Random
Line 2,828:
generatePasswords(pwdLen!!, pwdNum!!, toConsole, toFile!!)
}</
Sample input and output:
Line 2,860:
=={{header|Lua}}==
<
local index, pw, rnd = 0, ""
local chars = {
Line 2,889:
os.exit()
end
for i = 1, arg[2] do print(randPW(tonumber(arg[1]))) end</
Command line session:
<pre>>lua pwgen.lua
Line 2,908:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
and similar=1 if you want similar characters, 0 if not. True and \
False, should work in place of 1/0 *)
Line 2,938:
]</
=={{header|Nim}}==
<
const Symbols = toSeq("!\"#$%&'()*+,-./:;<=>?@[]^_{|}~")
Line 3,039:
# Display the passwords.
for pw in passGen(passLength, count, seed, excludeSimilars):
echo pw</
{{out}}
Line 3,052:
=={{header|OCaml}}==
<
let upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let digit = "0123456789"
Line 3,108:
for i = 1 to !num do
print_endline (mk_pwd !len !readable)
done</
{{out}}
Line 3,130:
=={{header|ooRexx}}==
{{trans||REXX}}
<
parse arg L N seed xxx dbg /*obtain optional arguments from the CL*/
casl= 'abcdefghijklmnopqrstuvwxyz' /*define lowercase alphabet. */
Line 3,227:
¦ dbg Schow count of characters in the 4 groups ¦
+-----------------------------------------------------------------------------+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~documentation ends on the previous line.~~~~~~~~~~~~~~~~~~~*/</
{{out}}
<pre>D:\>rexx genpwd 12 4 33 , x
Line 3,252:
PARI/GP has a very good builtin random generator.
<
{
if (len <= 4, print("password too short, minimum len=4"); return(), seed, setrand(seed));
Line 3,266:
}
addhelp(passwd, "passwd({len},{count},{seed}): Password generator, optional: len (min=4, default=8), count (default=1), seed (default=0: no seed)");</
Output: ''passwd()''<pre>34K76+mB</pre>
Line 3,286:
=={{header|Pascal}}==
<syntaxhighlight lang="pascal">
program passwords (input,output);
Line 3,463:
end.
</syntaxhighlight>
Useage for the output example: passwords --about -h --length=12 --number 12 --exclude
Line 3,498:
=={{header|Perl}}==
Use the module <tt>Math::Random</tt> for marginally better random-ness than the built-in function, but no warranty is expressed or implied, <i>caveat emptor</i>.
<
use warnings;
use feature 'say';
Line 3,547:
[--help]
END
}</
{{out}}
<pre>sc3O~3e0
Line 3,557:
=={{header|Phix}}==
<!--<
<span style="color: #000080;font-style:italic;">--with javascript_semantics -- not quite yet:</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (VALUECHANGED_CB not yet triggering)</span>
Line 3,614:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
With a length of 12 and generating 6 of them
Line 3,627:
=={{header|PicoLisp}}==
<
# Default seed
Line 3,690:
(rot '(*UppChars *Others *Digits *LowChars))) ) ) ) ) ) ) )
(bye)</
Test:
<pre>$ genpw --help
Line 3,711:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function New-RandomPassword
{
Line 3,831:
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
New-RandomPassword -Length 12 -Count 4 -ExcludeSimilar
</syntaxhighlight>
{{Out}}
<pre>
Line 3,843:
</pre>
Make it Unix-like:
<syntaxhighlight lang="powershell">
Set-Alias -Name nrp -Value New-RandomPassword -Description "Generates one or more passwords"
nrp -l 12 -n 4 -x
</syntaxhighlight>
{{Out}}
<pre>
Line 3,858:
=={{header|Prolog}}==
{{works with|SWI-Prolog|7.6.4 or higher}}
<
:- initialization(main, main).
Line 3,915:
pword_char( upper, C ) :- member( C, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ).
pword_char( digits, C ) :- member( C, "0123456789" ).
pword_char( special, C ) :- member( C, "!\"#$%&'()*+,-./:;<=>?@[]^_{|}~" ).</
{{out}}
Showing help
Line 3,951:
=={{header|PureBasic}}==
<
Procedure.b CheckPW(pw.s)
Line 4,072:
~"Blabla blabla bla blablabla."
EndOfHelp:
EndDataSection</
{{out}}
<pre>Length of the password (n>=4): 10
Line 4,093:
=={{header|Python}}==
<
lowercase = 'abcdefghijklmnopqrstuvwxyz' # same as string.ascii_lowercase
Line 4,127:
for i in range(qty):
print(new_password(length, readable))
</syntaxhighlight>
{{output}}
<pre>>>> password_generator(14, 4)
Line 4,141:
=={{header|R}}==
<syntaxhighlight lang="r">
passwords <- function(nl = 8, npw = 1, help = FALSE) {
if (help) return("gives npw passwords with nl characters each")
Line 4,166:
## Tj@T19L.q1;I*]
## 6M+{)xV?i|1UJ/
</syntaxhighlight>
=={{header|Racket}}==
<
#lang racket
Line 4,230:
(run (len) (cnt) (seed) (readable?))
</syntaxhighlight>
'''Sample output:'''
<pre>
Line 4,245:
{{works with|Rakudo|2016.05}}
<syntaxhighlight lang="raku"
set('a' .. 'z'),
set('A' .. 'Z'),
Line 4,278:
{$*PROGRAM-NAME} --l=14 --c=5 --x=0O\\\"\\\'1l\\\|I
END
}</
'''Sample output:'''
Using defaults:
Line 4,290:
===functional===
<syntaxhighlight lang="raku"
['a' .. 'z'],
['A' .. 'Z'],
Line 4,324:
Password must have at least one of each: lowercase letter, uppercase letter, digit, punctuation.
END
}</
'''Sample output:'''
Line 4,358:
:::* checking if the hexadecimal literal ('''yyy''') is valid
:::* checking (for instance) if all digits were excluded via the <b>'''xxx'''</b> and/or '''yyy''' option
<
@L='abcdefghijklmnopqrstuvwxyz'; @U=@L; upper @U /*define lower-, uppercase Latin chars.*/
@#= 0123456789 /* " " string of base ten numerals.*/
Line 4,413:
║ The default is to use all the (normal) available characters. ║
║ yyy (same as XXX, except the chars are expressed as hexadecimal pairs).║
╚═════════════════════════════════════════════════════════════════════════════╝ */</
'''output''' when using the inputs of: <tt> 10 20 </tt>
<pre>
Line 4,439:
=={{header|Ring}}==
<
# Project : Password generator
Line 4,484:
end
fclose(fp)
</syntaxhighlight>
Output:
<pre>
Line 4,494:
=={{header|Ruby}}==
<
("A".."Z").to_a,
("0".."9").to_a,
Line 4,510:
puts generate_pwd(8,3)
</syntaxhighlight>
=={{header|Run BASIC}}==
<
a$(2) = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a$(3) = "abcdefghijklmnopqrstuvwxyz"
Line 4,552:
goto [main]
[exit] ' get outta here
end</
<pre>Generate 10 passwords with 7 characters
#1 69+;Jj8
Line 4,565:
#10 f0Qho:5</pre>
=={{header|Rust}}==
<
use rand::distributions::Alphanumeric;
use rand::prelude::IteratorRandom;
Line 4,639:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 4,653:
=={{header|Scala}}==
Using SBT to run rather than a shell script or executable jar:
<
def newPassword( salt:String = "", length:Int = 13, strong:Boolean = true ) = {
Line 4,720:
if( count > 1 ) println
}</
{{output}}
> sbt "run --help"
Line 4,745:
=={{header|Seed7}}==
<
const func string: generate (in integer: length) is func
Line 4,800:
end if;
end if;
end func;</
{{out}}
Line 4,818:
Swift uses arc4random() to generate fast and high quality random numbers. However the usage of a user defined seed is not possible within arc4random(). To fulfill the requirements this code uses the C functions srand() and rand() that are integrated into the Swift file via an Bridging-Header.<br><br>
'''C file to generate random numbers'''
<
#include <time.h>
Line 4,832:
int getRand(const int upperBound){
return rand() % upperBound;
}</
'''Bridging-Header to include C file into Swift'''
<
void initRandom(const unsigned int seed);</
'''Swift file'''
<
import GameplayKit // for use of inbuilt Fisher-Yates-Shuffle
Line 4,962:
for i in 1...count {
print("\(i).\t\(generatePassword(length:length,exclude:xclude))")
}</
{{out}}
<pre>$ PasswordGenerator -h
Line 4,981:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Sub Main()
Line 5,179:
End If
z = temp
End Function</
{{out}}
Function Gp :
Line 5,228:
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<
import "/ioutil" for FileUtil, File, Input
import "/fmt" for Fmt
Line 5,362:
}
generatePasswords.call(pwdLen, pwdNum, toConsole, toFile)</
{{out}}
Line 5,396:
=={{header|zkl}}==
Put the following code into a file (such as pwdg.zkl):
<
argh:=Utils.Argh(
Line 5,417:
pwd:=T(g1,g2,g3,g4).pump(Data,rnd); // 1 from each of these into a Data
pwd.extend(fill()).shuffle().text.println();
}</
This is a command line program so output can be redirected.
{{out}}
|