crypt :: String -> String -> String
crypt key salt =
taketail 10 $
unsafePerformIO $ peekCString $
unsafePerformIO $
withCString key $ \k ->
withCString salt $ \s ->
c_crypt k s
where
taketail :: Int -> [a] -> [a]
taketail i x = (reverse (take i (reverse x)))
makesalt :: String -> String
makesalt x =
map
fixsalt
(take 2
(if (length x) < 2 then ("H.") else (tail x)))
where
fixsalt :: Char -> Char
fixsalt c
| (inrange '0' '9' c) = c
| (inrange ':' '@' c) = (chr ((ord c) + 7))
| (inrange 'A' 'Z' c) = c
| (inrange '[' '`' c) = (chr ((ord c) + 6))
| (inrange 'a' 'z' c) = c
| c == '/' || c == '.' = c
| otherwise = '.'
where
inrange :: Ord a => a -> a -> a -> Bool
inrange low upp x = x >= low && x <= upp
tripcode :: String -> String
tripcode s = crypt s (makesalt s)
main :: IO ()
main = do
args <- getArgs
putStrLn (tripcode (head args))
Name:
Anonymous2006-11-04 7:12
Real men would do without unsafePerformIO. Plus your indent style gives me hives.
Still, this has got to be the most concise expression of the tripcode algorithm I've seen yet.
>>3
Doubt it. If there were one in the standard library, its definition would likely be exactly as you wrote (only more general with the types, of course).
>>3
Ooooooor you could do something with "drop" and "length". That's just one complete pass, instead of two reverses. Not that this kind of a mickey mouse optimization matters in this case...
Name:
Anonymous2006-11-04 9:29
If n is list length and k is take length then taketail i x = (reverse (take i (reverse x)))has a time cost of roughly n + 2k taketail3 i x =
let d = max 0 (length x - i)
in drop d xhas a time cost of roughly 2n - k
Still, this has got to be the most concise expression of the tripcode algorithm I've seen yet.
it's a much simplified version of the tripcode algorithm, and it's not all that concise...
perl1: #!/usr/bin/perl
(($salt=substr $ARGV[0].'H..',1,2)=~tr/:;<=>?@[\\]^_`/ABCDEFGabcdef/)=~s/[^\.-z]/./g;
print substr(crypt($ARGV[0],$salt),-10),"\n";
c: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/des.h>
int main(int argc, char *argv[]) {
const char saltchars[]=":;<=>?@[\\]^_`ABCDEFGabcdef";
char key[16]={0};
if(argc>1) strncpy(key,argv[1],8);
else exit(1);
char salt[3]={0};
char trip[11]={0};
int i=0;
int j;
strncpy(salt,key+1,2);
if(strlen(key)==2) salt[1]='H';
if(strlen(key)<2) {
salt[0]='H';
salt[1]='.'; }
if(strlen(key)==0) salt[0]='.';
for(i=0;i<2;i++) for(j=0;j<13;j++) if(salt[i]==saltchars[j]) salt[i]=saltchars[j+13];
if(salt[0]<'0'||salt[0]>'z') salt[0]='.';
if(salt[1]<'0'||salt[1]>'z') salt[1]='.';
memcpy(trip,DES_crypt(key,salt)+3,10);
puts(trip);
return(0); }
>>8
But these lack the clarity of the Haskell version. (Also, char salt[3]={0}; doesn't do what you might think it did, though the result is the same. For instance, char salt[3]={42}; would set the first element to 42 and the rest to 0.)
Only a functional programming nut could call that code "clear" or "consice".
Name:
Anonymous2006-11-04 13:01
>>9
What? = {0} is used to initialize arrays with zeros, nothing else.
Name:
Anonymous2006-11-04 13:04
javascript ftw.
first, include http://javascript.internet.com/passwords/javacrypt.js.
then, do this: String.prototype.translate = function (s1, s2) {
var o = new Object();
for (i=0; i<s1.length; i++) o[s1[i]] = s2[i];
var a=this;
for (i=0; i<a.length; i++) if (o[a[i]]) a[i] = o[a[i]];
return a;
};
alert(Javacrypt.crypt(((key=prompt('key?'))+'H..').substring(1,3).replace(/[^\.-z]/g,'.').translate(':;<=>?@[\\]^_`','ABCDEFGabcdef'),key)[0].substring(3));
>>27
Yes yes, let's all reimplement standard UNIX libc calls for every last program. That's so much more readable and has absolutely no chance at all of repeating the same bugs over and over!
Name:
Anonymous2006-11-15 18:02
Péter pue.
Name:
Anonymous2006-11-17 15:50
Perl sucks because there's a hundred ways of doing any given task. Haskell, on the other hand, fucking rocks!!
s zzs vvxv
and s ZVZchr 122Zie
and s ZVZchr 122Zie
and s ZVZchr 122Zie
and s SxSlcfirstSe
and s YZZxZYvvxvYi and print
z
and s ZVZchr 122Zie
and s ZVZchr 122Zie
and s ZVZchr 122Zie
and s SxSlcfirstSe
and s YZZxZYvvxvYi and print