以ä¸ãç解ãããã¨ããã¨ãã®ã¡ã¢ï¼
- Efficient Denial of Service Attacks on Web Application Platforms
- n.runs-SA-2011.004 - web programming languages and platforms - DoS through hash table
æ¦è¦
å¤é¨ããã®ãã¼ã¿ã«å¯¾ãã¦ãå¹çã®æªãã¢ã«ã´ãªãºã ã§å¦çããã¨ã»ãã¥ãªãã£ãã¼ã«ã«ãªãã¨ããä¸è¬çãªåé¡ã®ä¸ä¾ãNè¦ç´ ã®ããã·ã¥ãã¼ãã«ã®ä½æã¯ãå¹³åã§O(N)ã ããææªã§ O(N^2) ã¨ãªãã
ããããªè¨èªã§ãDJB ã®ããã·ã¥ãå©ç¨ããã¦ããã
- DJBX33A
- Equivalent string ã§è¡çªãããããä½ãããç解ã¯å®¹æã
- DJBX33X
- ä¸éä¸è´(Meet-in-the-middle)æ»æã§è¡çªãããããçºè¦ã§ããããã®ç解ã¯å°ãé£ããã
- Haskell ã® hashable ããã±ã¼ã¸ã¯ãã£ã¡ã使ã£ã¦ããã
DJBX33X
XOR ã使ã£ã¦ããã®ã§ãåºæ¬çã«ç·å½ããæ»æã§è¡çªãçºè¦ãããä¸éä¸è´æ»æã使ãã¨ãè¨ç®éãæ ¼æ®µã«ä½æ¸ã§ãããä¸éä¸è´æ»æãå®ç¾ããããã«ã¯ãDJBX33X ã®éé¢æ°ãå¿ è¦ã
ããéé¢æ°ããããªãã以ä¸ã®ããã«ä¸éä¸è´æ»æãå®ç¾ã§ãããå®éã«ä½¿ããã¦ããåæå¤ã調ã¹ããé©å½ã«æçµçãªå¤ã決ããããã®å¤ãè¡çªããå¤ã3æåç¨åº¦ã®ã©ã³ãã ãªæåå(suffix ã¨å¼ã¶)ãããããç¨æããæçµçãªå¤ã¨æååããéé¢æ°ã使ã£ã¦ä¸éå¤ãããããæ±ãããè¡çªããããã©ãæ¦ãç¨æããæååã®æ°ãä¸éå¤ãæ±ã¾ãã
7æåç¨åº¦ã®ã©ã³ãã ãªæåå(prefixã¨å¼ã¶)ãããããç¨æãããåæå¤ã¨æååãã DJBX33X ã使ã£ã¦ããã·ã¥å¤ãè¨ç®ããä¸éå¤ã®ä¸ã«ä¸è´ãããã®ã調ã¹ããä¸è´ããã°ãprefix+suffix ãå¾ããæååããã®æååã¯ããããçºè¦ã§ãããã¹ã¦åæå¤ããåãæçµçãªå¤ãçæãããã¤ã¾ããè¡çªããæååãããããæã«å ¥ãã
å®éã« Haskell ã§ã³ã¼ããæ¸ãã¦ã¿ãããé©ãã»ã©ããããè¦ã¤ãã£ãã
DJBX33X ã®éé¢æ°
f à g = 1 ãæºããã®ããf ã®éé¢æ° gã
DJBX33X ãæ°å¤å¼ã§è¡¨ãã¨ï¼
h_n+1 = (33 h_n) xor x (mod 2^m)
A xor B xor B = A ã ããã両辺㫠xor x ãå³ããæ½ãã¨ï¼
h_n+1 xor x = 33 h_n (mod 2^m)
ãã¨ã¯ãæ³ 2^m ã«ããã 33 ã®éå ãæ±ããã°ãããããã¯æ¡å¼µã¦ã¼ã¯ãªããã®äºé¤æ³ãç¨ããã°ãç°¡åã«è¦ã¤ãããããã (1/33) ã¨è¡¨ç¾ããã¨ï¼
(h_n+1 xor x) Ã (1/33) = h_n (mod 2^m)
ãã£ã¦éé¢æ°ãæ±ã¾ã£ãã
ãµã¼ãæ»æ
ãã©ã¼ã ã®ãã¼ã¿ã¨ã㦠key 㨠value ã®çµã POST ããããã® key ã«æ±ããããããã®æååã使ãããµã¼ãã¯èªåçã«ããã·ã¥ãã¼ãã«ãçæããã®ã§ãCPU æéãããããæ¶è²»ããã
Haskell
Haskell ã§ã¯ãããã·ã¥ãã¼ãã«(è¾æ¸ã¨ããæ¹ãé©å)ã®å®è£ ã«æ¨ãç¨ãããã¦ããã®ã§ãåºæ¬çã«ãã®æ»æã«ã¯å¼±ããªããã¤ã¾ããData.Map ã Data.IntMap ã¯å¹³åã§ãææªã®å ´åã§ã O(N log N)ã
hashable ããã±ã¼ã¸ã® Hashable ã¯ã©ã¹ãå©ç¨ãã¦ãã hashmap ã® Map ã unordered-containers ã® HashMap ã¯ãDJBX33X ã使ããã¨ã«ãªãã
- hashmap ã® Map ã¯ãæ¨ã§ãããæ«ç«¯ãæ¨(Data.Map)
- ãã¹ã¦ã®è¦ç´ ãè¡çªãããªããæ¨ã¯æé·ããªãã®ã§ O(1)
- è¡çªããè¦ç´ ã®æ¿å ¥ã¯ O(log N)
- ãã£ã¦ãææª O(N log N) ã¨ãªã
- unordered-containers ã® HashMap ã¯ãæ¨ã§ããããæ«ç«¯ã¯ãªã¹ã(FullList)
- ãã¹ã¦ã®è¦ç´ ãè¡çªãããªããæ¨ã¯æé·ããªãã®ã§ O(1)
- è¡çªããè¦ç´ ã®æ¿å ¥ã¯ O(N)
- ãã£ã¦ãææª O(N^2) ã¨ãªã
対ç
Hashable ã® hash ã§ã¯ãªã hashWithSalt ã¨ã©ã³ãã 㪠Salt ã使ããSalt ã¨ã¯åæå¤ãåæå¤ãæ¨æ¸¬ã§ããªããã°ãè¡çªããæååãçºè¦ããã®ã¯å°é£ãJohan ã«ããã°ã以ä¸ã®ããã«ãã¦ç§å¯ã®(ã©ã³ãã ãª) secretSalt 㨠mix ãå®ç¾©ããã°ããã®ã§ã¯ãªããã¨ã®ãã¨ã
newtype SaltedString = SS ByteString instance Hashable SaltedString where hashWithSalt salt (SS str) = hashWithSalt (secretSalt `mix` salt) str