OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 try: |
| 7 import hashlib |
| 8 _new_md5 = hashlib.md5 |
| 9 except ImportError: |
| 10 import md5 |
| 11 _new_md5 = md5.new |
| 12 |
| 13 """64-bit fingerprint support for strings. |
| 14 |
| 15 Usage: |
| 16 from extern import FP |
| 17 print 'Fingerprint is %ld' % FP.FingerPrint('Hello world!') |
| 18 """ |
| 19 |
| 20 |
| 21 def UnsignedFingerPrint(str, encoding='utf-8'): |
| 22 """Generate a 64-bit fingerprint by taking the first half of the md5 |
| 23 of the string.""" |
| 24 hex128 = _new_md5(str).hexdigest() |
| 25 int64 = long(hex128[:16], 16) |
| 26 return int64 |
| 27 |
| 28 def FingerPrint(str, encoding='utf-8'): |
| 29 fp = UnsignedFingerPrint(str, encoding=encoding) |
| 30 # interpret fingerprint as signed longs |
| 31 if fp & 0x8000000000000000L: |
| 32 fp = - ((~fp & 0xFFFFFFFFFFFFFFFFL) + 1) |
| 33 return fp |
| 34 |
OLD | NEW |