'===========================================================================
' Subject: ONE-WAY PASSWORD ENCODER           Date: 04-10-00 (11:22)       
'  Author: Andreas van Cranenburgh            Code: QB, QBasic, PDS        
'  Origin: andreaswolf@mail.com             Packet: MISC.ABC
'===========================================================================
'One-Way password encoder.                By Andreas van Cranenburgh (C) 2000
'    Mail any suggestions and bug reports to andreaswolf@mail.com
'This program returns a (probably) unique number for each password. It uses
'a formula wich can not be reversed (I guess). You can easily make password
'protected programs with this FUNCTION. If the password is only 1 char long
'(wich would be very stupid!) it will return the ascii code plus 1, I don't
'know any solution for this problem. I believe Linux uses a comparable method
'of checking passwords.
DEFINT A-Z
DECLARE FUNCTION PassCode# (Text$)
CLS
DO
  Text$ = ""
  PRINT "Password: ";
  DO
    DO
      k$ = INKEY$
    LOOP UNTIL LEN(k$)
    IF k$ = CHR$(13) THEN EXIT DO
    IF k$ = CHR$(8) THEN
      Text$ = LEFT$(Text$, LEN(Text$) - 1)
      LOCATE , POS(0) - 1
      PRINT " ";
      LOCATE , POS(0) - 1
    ELSE
      PRINT "*";
      Text$ = Text$ + k$
    END IF
  LOOP
  PRINT
  PRINT "Pass-Code(C): "; PassCode#(Text$)
LOOP UNTIL Text$ = ""
END

DEFDBL A-Z
FUNCTION PassCode# (Text$)
IF Text$ = "" THEN EXIT FUNCTION
TextLen = LEN(Text$)
IF TextLen AND 1 THEN
  TextLen = TextLen - 1
  IsOdd = -1
END IF
FOR a = 1 TO LEN(Text$)
  Total = Total + ASC(MID$(Text$, a, 1))
NEXT a
FOR a = 1 TO TextLen \ 2
  b = TextLen - a
  StepOne = StepOne + (ASC(MID$(Text$, a, 1)) * ASC(MID$(Text$, b, 1)))
NEXT a
IF IsOdd THEN StepOne = StepOne + ASC(RIGHT$(Text$, 1))
StepOne = StepOne / Total
FOR a = 1 TO TextLen STEP 2
  StepTwo = StepTwo + (ASC(MID$(Text$, a, 1)) ^ 2) / ASC(MID$(Text$, a + 1, 1))
NEXT a
IF IsOdd THEN StepTwo = StepTwo / ASC(RIGHT$(Text$, a))

FOR a = 1 TO LEN(Text$)
  StepThree = StepThree + (ASC(MID$(Text$, a, 1)) ^ 2)
NEXT a
StepThree = StepThree ^ .5                'Get square root

OutCome = StepOne + StepTwo + StepThree
PassCode# = OutCome
END FUNCTION
