http://groups.umd.umich.edu/cis/course.des/cis400/forth/arcfour.html

Description

This is an implementation of the high-strength ArcFour encryption algorithm, written using the Quartus Forth on-board compiler for the Palm series of handheld computers.

Source Code

\\ arcfour implementation
needs core-ext

decimal
: carray ( n "name" -- )
  create  chars allot
  does> ( n -- addr )  swap chars + ;

256 carray S

0 value j
: j+! ( n -- )  j +  255 and  to j ;

: init-S ( -- )  256 0 do  i dup S c!  loop ;

: mix-S ( K-addr length -- )
  256 0 do
\\ j=j+S[i]+K[i modulo length]
    2dup  i swap mod chars +  c@
    i S c@  +  j+!
\\ swap S[i] and S[j]
    j S c@  i S c@  j S c!  i S c!
  loop  2drop ;

: arcfourkey ( K-addr length -- )
  init-S  0 to j  mix-S ;

: arcfour ( M-addr length -- )
  0 to j
  swap 1- swap
  1+ 1 do
\\ j=j+S[i]
    i S c@ j+!
\\ swap S[i] and S[j]
    j S c@  i S c@  2dup  j S c!  i S c!
\\ xor M[i-1] with S[j]+S[i]
    + 255 and S  c@  over i chars +  dup
    c@ rot xor  swap c!
  loop  drop ;

create MD5digest 16 chars allot
: MD5 ( c-addr u -- c-addr2 16 )
  MD5digest >abs  2swap  swap >abs
  EncDigestMD5  2drop ( * fix * )
  MD5digest 16 ;

Program Notes

The 'needs core-ext' at the top of the file is specific to Quartus Forth, and the MD5 definition at the bottom of the file calls a specific routine in the Palm OS; the rest of the file, however, is Standard Forth.