Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Shamless request for improving function speed.

From: reboler::programmer::net at: 26-Mar-2002 15:05

This group is really good at tweaking functions to make them faster. This is a shameless request for help making the following functions _faster_. {not necessarily shorter, just faster :)} Any help would be deeply appreciated. The second set (object) is a REBOL version of Ron Rivest's (formerly of RSA) RC4 encryption algorithm, used in some secure internet transactions. ;***** begin first function set comp-pass: func [ {Compress a string with a passphrase. Only casually secure! Needs DECOMP-PASS function and passphrase to decompress} text [string! binary!] {Data to compress with a passphrase. Requires 'decomp-pass to decompress.} passphrase [string! binary!] {passphrase to compress the data with} /local comp-text p ][ comp-text: compress text p: 0 for n 3 ((length? comp-text) - 4) 1 [ poke comp-text n to-char xor (pick comp-text n) (pick passphrase (p + 1)) p: (p + 1) // (length? passphrase) ] comp-text ] decomp-pass: func [ {Decompress a string compressed with 'comp-pass, requires passphrase.} comp-text [binary!] {Data to decompress with a passphrase} passphrase [string! binary!] {passphrase to decompress the data with} /local text p ][ p: 0 for n 3 ((length? comp-text) - 4) 1[ poke comp-text n to-char xor (pick comp-text n) (pick passphrase (p + 1)) p: (p + 1) // (length? passphrase) ] either error? try [text: decompress comp-text][ "** decomp-pass error ** Wrong passphrase? " ][ text ] ] ;***** set two ******** arc4: make object! [ s-swap: func [{swaps the elements of two series at the given index positions} a [series!] "1st series" index-a [integer!] "index position to swap in 1st series" b [series!] "2nd series" index-b [integer!] "index position to swap in 2nd series" /local holder ][ holder: pick a index-a poke a index-a pick b index-b poke b index-b holder ] get-key: func [key] [ while [(length? key) > 256] [ print "Key must be 256 characters or less, with IV" key: ask/hide "key :>> " ] key ] mix-state: func [key /local j n] [ j: 0 state: copy make block! 256 for i 0 255 1 [append state i] if not value? 'iterations [iterations: 1] loop iterations [ for i 0 255 1 [ n: i // (length? key) state-i: to-integer pick state (i + 1) key-n: to-integer pick key (n + 1) j: (j + state-i + key-n) // 256 s-swap state (i + 1) state (j + 1) ] ] ] produce-ciphertext: func [plaintext key /local j n] [ j: 0 ciphertext: make string! length? plaintext for count 0 ((length? plaintext) - 1) 1 [ i: ((count + 1) // 256) state-i: pick state (i + 1) j: ((j + state-i) // 256) s-swap state (i + 1) state (j + 1) n: (((pick state (i + 1)) + (pick state (j + 1))) // 256) append ciphertext (xor (to-char pick state (n + 1)) (to-char pick plaintext (count + 1))) ] clear state ciphertext ] arc4: func [{ARCFour Function to encrypt/decrypt a string! or binary!. The function should be given a string! or binary!, and outputs a string!. "arcfour plaintext key" will encrypt, "arcfour ciphertext key" decrypts} plaintext [string! binary!] "String to en/decrypt." key [string! binary!] {Supply a key [string! binary!].} ][ mix-state get-key key produce-ciphertext plaintext key unset [plaintext key iterations] ciphertext ] ] ;***** end If you use these, please note that you will have to convert the output of decrypted data back to 'binary! yourself, in such cases as when the original data was 'binary!. -- _______________________________________________ Sign-up for your own FREE Personalized E-mail at Mail.com http://www.mail.com/?sr=signup Win the Ultimate Hawaiian Experience from Travelocity. http://ad.doubleclick.net/clk;4018363;6991039;n?http://svc.travelocity.com/promos/winhawaii/