OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # |
| 3 # =================================================================== |
| 4 # The contents of this file are dedicated to the public domain. To |
| 5 # the extent that dedication to the public domain is not available, |
| 6 # everyone is granted a worldwide, perpetual, royalty-free, |
| 7 # non-exclusive license to exercise all rights associated with the |
| 8 # contents of this file for any purpose whatsoever. |
| 9 # No rights are reserved. |
| 10 # |
| 11 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 12 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 13 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 14 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 15 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 16 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 17 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 # SOFTWARE. |
| 19 # =================================================================== |
| 20 |
| 21 from binascii import hexlify |
| 22 |
| 23 class HashAlgo: |
| 24 """A generic class for an abstract cryptographic hash algorithm. |
| 25 |
| 26 :undocumented: block_size |
| 27 """ |
| 28 |
| 29 #: The size of the resulting hash in bytes. |
| 30 digest_size = None |
| 31 #: The internal block size of the hash algorithm in bytes. |
| 32 block_size = None |
| 33 |
| 34 def __init__(self, hashFactory, data=None): |
| 35 """Initialize the hash object. |
| 36 |
| 37 :Parameters: |
| 38 hashFactory : callable |
| 39 An object that will generate the actual hash implementation. |
| 40 *hashFactory* must have a *new()* method, or must be directly |
| 41 callable. |
| 42 data : byte string |
| 43 The very first chunk of the message to hash. |
| 44 It is equivalent to an early call to `update()`. |
| 45 """ |
| 46 if hasattr(hashFactory, 'new'): |
| 47 self._hash = hashFactory.new() |
| 48 else: |
| 49 self._hash = hashFactory() |
| 50 if data: |
| 51 self.update(data) |
| 52 |
| 53 def update(self, data): |
| 54 """Continue hashing of a message by consuming the next chunk of data. |
| 55 |
| 56 Repeated calls are equivalent to a single call with the concatenation |
| 57 of all the arguments. In other words: |
| 58 |
| 59 >>> m.update(a); m.update(b) |
| 60 |
| 61 is equivalent to: |
| 62 |
| 63 >>> m.update(a+b) |
| 64 |
| 65 :Parameters: |
| 66 data : byte string |
| 67 The next chunk of the message being hashed. |
| 68 """ |
| 69 return self._hash.update(data) |
| 70 |
| 71 def digest(self): |
| 72 """Return the **binary** (non-printable) digest of the message that has
been hashed so far. |
| 73 |
| 74 This method does not change the state of the hash object. |
| 75 You can continue updating the object after calling this function. |
| 76 |
| 77 :Return: A byte string of `digest_size` bytes. It may contain non-ASCII |
| 78 characters, including null bytes. |
| 79 """ |
| 80 return self._hash.digest() |
| 81 |
| 82 def hexdigest(self): |
| 83 """Return the **printable** digest of the message that has been hashed s
o far. |
| 84 |
| 85 This method does not change the state of the hash object. |
| 86 |
| 87 :Return: A string of 2* `digest_size` characters. It contains only |
| 88 hexadecimal ASCII digits. |
| 89 """ |
| 90 return self._hash.hexdigest() |
| 91 |
| 92 def copy(self): |
| 93 """Return a copy ("clone") of the hash object. |
| 94 |
| 95 The copy will have the same internal state as the original hash |
| 96 object. |
| 97 This can be used to efficiently compute the digests of strings that |
| 98 share a common initial substring. |
| 99 |
| 100 :Return: A hash object of the same type |
| 101 """ |
| 102 return self._hash.copy() |
| 103 |
| 104 def new(self, data=None): |
| 105 """Return a fresh instance of the hash object. |
| 106 |
| 107 Unlike the `copy` method, the internal state of the object is empty. |
| 108 |
| 109 :Parameters: |
| 110 data : byte string |
| 111 The next chunk of the message being hashed. |
| 112 |
| 113 :Return: A hash object of the same type |
| 114 """ |
| 115 pass |
| 116 |
OLD | NEW |