Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #26479

Re: random password generator

From Bernd Paysan <bernd.paysan@gmx.de>
Newsgroups comp.lang.forth
Subject Re: random password generator
Date 2013-10-13 20:32 +0200
Organization 1&1 Internet AG
Message-ID <l3ep0h$43g$1@online.de> (permalink)
References <7x38o5zzwz.fsf@ruckus.brouhaha.com>

Show all headers | View raw


Paul Rubin wrote:

> #! /usr/bin/gforth
> 12 constant password_length
> variable rfile
> create buf 1 chars allot
> 
> s" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_="
> ( addr u ) assert( dup 64 = ) 2drop constant alphabet
> 
> : chk ( n -- )  assert( dup = ) drop ;
> : tchar ( c -- ) $3f AND alphabet + c@ emit ;
> : open-rand.1 ( -- wfileid ) s" /dev/urandom" r/o open-file 0 chk ;
> : open-rand ( -- ) open-rand.1 rfile ! ;
> : randombyte ( -- c ) buf 1 rfile @ read-file 0 chk 1 chk buf c@ ;
> : run ( -- ) open-rand   password_length 0 do randombyte tchar loop cr ;
> 
> run bye

This is a script.  Things you just need to do once (e.g. open /dev/random) 
don't need to go into a definition.  Just do them in interactive mode.  Use 
throw on iors.  If you have questions whether your alphabet really is 64 
characters, add a ~~ right after the string once for debugging.

Here's how I would do it:

#! /usr/bin/gforth

s" /dev/urandom" r/o open-file throw value rndfd
: randombyte ( -- c )  rndfd key-file ;
: >alphabet ( n -- c )  $3F and
  s" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_="
  drop + c@ ;
: passwd ( len -- )  0 ?DO  randombyte >alphabet emit  LOOP cr ;

12 passwd bye

If you want to make that more robust for different alphabets (some passwords 
may have only digits and letters in it, or may not have uppercase letters to 
avoid confusion and whatever...), better do that:

#! /usr/bin/gforth

s" /dev/urandom" r/o open-file throw value rndfd
: randomword ( -- u16 ) rndfd key-file rndfd key-file 8 lshift + ;
: >alphabet ( u16 -- c )
  s" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_="
  rot * 16 rshift + c@ ;
: passwd ( len -- )  0 ?DO  randomword >alphabet emit  LOOP cr ;

12 passwd bye

BTW: Test what you have written, word by word.  Your chk does not work, 
because "dup =" is always true.

There is a reason why my >alphabet does neither contain the emit: This 
allows you to write checks for the word.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

random password generator Paul Rubin <no.email@nospam.invalid> - 2013-10-12 19:07 -0700
  Re: random password generator hughaguilar96@yahoo.com - 2013-10-12 22:01 -0700
  Re: random password generator Paul Rubin <no.email@nospam.invalid> - 2013-10-12 22:32 -0700
    Re: random password generator hughaguilar96@yahoo.com - 2013-10-12 22:44 -0700
  Re: random password generator "WJ" <w_a_x_man@yahoo.com> - 2013-10-13 08:23 +0000
    Re: random password generator Paul Rubin <no.email@nospam.invalid> - 2013-10-13 05:46 -0700
    Re: random password generator "WJ" <w_a_x_man@yahoo.com> - 2013-10-13 17:09 +0000
  Re: random password generator Bernd Paysan <bernd.paysan@gmx.de> - 2013-10-13 20:32 +0200
    Re: random password generator Paul Rubin <no.email@nospam.invalid> - 2013-10-13 12:27 -0700
      Re: random password generator Bernd Paysan <bernd.paysan@gmx.de> - 2013-10-13 22:52 +0200
        Re: random password generator Paul Rubin <no.email@nospam.invalid> - 2013-10-13 14:43 -0700

csiph-web