Chromium ID 计算方法

 1 try:
 2   import hashlib
 3   _new_md5 = hashlib.md5
 4 except ImportError:
 5   import md5
 6   _new_md5 = md5.new
 7 
 8 def _UnsignedFingerPrintImpl(str, encoding='utf-8'):
 9   hex128 = _new_md5(str).hexdigest()
10   int64 = long(hex128[:16], 16)
11   return int64
12 
13 
14 def UnsignedFingerPrint(str, encoding='utf-8'):
15   return _UnsignedFingerPrintImpl(str, encoding)
16 
17 
18 def FingerPrint(str, encoding='utf-8'):
19   fp = UnsignedFingerPrint(str, encoding=encoding)
20   # interpret fingerprint as signed longs
21   if fp & 0x8000000000000000L:
22     fp = - ((~fp & 0xFFFFFFFFFFFFFFFFL) + 1)
23   return fp
24   
25 def GenerateMessageId(message, meaning=''):
26   fp = FingerPrint(message)
27   if meaning:
28     # combine the fingerprints of message and meaning
29     fp2 = FingerPrint(meaning)
30     if fp < 0:
31       fp = fp2 + (fp << 1) + 1
32     else:
33       fp = fp2 + (fp << 1)
34   # To avoid negative ids we strip the high-order bit
35   #SILVERAEGIS_BEGIN_BEGIN
36   fpid = str(fp & 0x7fffffffffffffffL)
37   return fpid
38 test = GenerateMessageId("Adding to BlackSwan ...")
39 #test = GenerateMessageId('&Show in Finder', "'show in Finder' on Mac in Title Case")
40 print test

文章出处: http://www.cnblogs.com/zhfuliang/archive/2013/05/21/3090938.html

posted @ 2013-05-21 16:09  小小亮FLY  阅读(276)  评论(0)    收藏  举报