Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

hashing algorithm

Name: hasher 2009-06-22 12:14

Hello, I need an hashing algorithm which outputs a hash which doesn't change much if the output doesn't change much.
Ideally if I had string1 which were quite similar to string2, the two hashes should differ by something comparable to the Levenshtein distance.
That's not really important though.
I thought about an homothetic transformation (that is taking each nth character in order to end up with a constant length string), but I don't really know.
Any suggestions?

Name: Anonymous 2009-06-23 15:34

Tell me how this works:
module WhatHash where

import Char
import Numeric

{-
The [i]homoerotic[/i] hashing algorithm requested in http://dis.4chan.org/read/prog/1245687289
-}

data Binary = Binary [Bool] deriving Eq

xor True = not
xor False = id

pow2s = map (2^) [0..]

intToBin 0 = []
intToBin x = reverse $ que (-1) what
         where que l (y:ys) | y == 0    = False:que y ys
                            | y == x    = [True]
                            | y == l    = False:que y ys
                            | otherwise = True:que y ys
               what = map (mod x) $ tail pow2s

binToInt xs' = binToInt' 0
          where xs = reverse xs'
                binToInt' n | n >= length xs = 0
                            | xs !! n        = (pow2s !! n) + binToInt' (n+1)
                            | otherwise      = binToInt' (n+1)

padl n p list | length list >= n = list
              | otherwise        = padl n p (p:list)

charToByte = padl 8 False . intToBin . ord
byteToChar = chr . binToInt

instance Show Binary where
    show (Binary [])         = ""
    show (Binary (True:xs))  = '1':show (Binary xs)
    show (Binary (False:xs)) = '0':show (Binary xs)

instance Read Binary where
    readsPrec _ xs = [(Binary $ map q xs,"")]
          where q '1' = True
                q _   = False

splitEvery n xs | length next > 0 = this:splitEvery n next
                | otherwise       = [this]
              where (this,next) = splitAt n xs

--(accumulate-n) from SICP Exercise 2.36
foldrn f n []     = []
foldrn f n ([]:_) = []
foldrn f n xs     = foldr f n (q head xs):foldrn f n (q tail xs)
    where q ad = map ad . filter (/=[])

whatHash = foldrn xor True . splitEvery 128

hashStrToBin = whatHash . concatMap charToByte . padl 16 ' '

hashStrToBytes = map byteToChar . splitEvery 8 . hashStrToBin

hashStrToStr = foldr showHex "" . map binToInt . splitEvery 8 . hashStrToBin

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List