[comp.lang.pascal] Password echoing

ZCCBJSB%EB0UB011.BITNET@cunyvm.cuny.edu (Josep Sau B.) (04/10/91)

John Garay <garay@hyper.lap.upenn.edu> sez':

>I'm interested in a procedure in TP which will echo dots
>to the screen instead of the actual keys pressed.
>... more lines deleted!

As far as I know, there is no immediate way to do this using
only a brute force ReadLn (STRING).
When I needed something simmilar in a programa I did some time
ago, I had to build my own ReadPasswordLn FUNCTION echoing '*',
using ReadKey inside a REPEAT...UNTIL Loop.

Something like this (no syntax check, written from scratch):

CONST
  NULSTR = '';
  CR = CHR(13);
  ESC = CHR(27);
  BELL = CHR(7);
  PwMaxLen = 8;

TYPE
  Password = STRINGMaxPasswordLen!;

FUNCTION ReadPasswordLn :Password;
  VAR
    ch :CHAR;
    pw :Pasword;
    Done :BOOLEAN;
  BEGIN
    pw := NULSTR;
    Done := FALSE;
    REPEAT
      ch := ReadKey;
      CASE ch OF         (* assumes  Legal char A-Z0-9_! *)
        'A'..'Z','0'..'9','_':
          BEGIN
            write('*');
            pw := pw + Upcase(ch);
          END;
        CR: Done := TRUE;        (* Validates entered line *)
        ESC:                     (* Cancels whole entered line *)
          BEGIN
            pw := NULSTR;
            Done := TRUE;
          END;
        ELSE Write(BELL);  (* key rejected *)
      END;
      Done := Done OR (LENGTH(pw) = MaxPasswordLen);
    UNTIL Done;
    ReadPasswordLn := pw;
  END; (* FUNCTION ReadPasswordLn *)

(* Other enhancements should be added as BackSpace,
   and not exiting automatically the edit process
   if MaxPasswordLne is reached...*)

If anybody knows some way to do it easier I'd also like to know.

Sometime ago somebody proposed other tricks:
- Before call ReadLn, make TextColour = TextBackground,
  this makes input invisible on screen (no dots),
  but a Ctrl-PrtScreen will still dump the password to printer...
- Another one proposed to redirect output to another file,
  not to StdOutput (screen), but then no dots are output either.

--Josep Sau (ZCCBJSB@EB0UB011).

P.S. What about editing mails with maximum width 70 char ?
     It's really annoying to see those lines awfully splitted
     on screen...