Skip to content

Instantly share code, notes, and snippets.

@frohoff
Last active December 13, 2023 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frohoff/3875a68a9be580a3dee9 to your computer and use it in GitHub Desktop.
Save frohoff/3875a68a9be580a3dee9 to your computer and use it in GitHub Desktop.
terse ruby rc4 (161 chars, 154 chars not counting proc/param overhead) for https://twitter.com/matthew_d_green/status/524966294492577792
->t,k{s=*0..255;j=0;m=256;m.times{|i|j=(j+s[i]+k[i%k.size])%m;s[i],s[j]=s[j],s[i]};i=j=0;t.map{|b|i=(i+1)%m;j=(j+s[i])%m;s[i],s[j]=s[j],s[i];b^s[(s[i]+s[j])%m]}}
@frohoff
Copy link
Author

frohoff commented Oct 22, 2014

pry(main)> ->t,k{s=*0..255;j=0;m=256;m.times{|i|j=(j+s[i]+k[i%k.size])%m;s[i],s[j]=s[j],s[i]};i=j=0;t.map{|b|i=(i+1)%m;j=(j+s[i])%m;s[i],s[j]=s[j],s[i];b^s[(s[i]+s[j])%m]}}[[1,2,3,4],[5,6,7,8]]
=> [247, 228, 31, 141]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment