|
| 1 | +*************************** |
| 2 | +The hash-algorithms Library |
| 3 | +*************************** |
| 4 | + |
| 5 | +.. current-library:: hash-algorithms |
| 6 | +.. current-module:: hash-algorithms |
| 7 | + |
| 8 | +The hash-algorithms library provides consistent access to a variety of |
| 9 | +hash algorithms: |
| 10 | + |
| 11 | +* md5 |
| 12 | +* sha1 |
| 13 | +* sha224 |
| 14 | +* sha256 |
| 15 | +* sha384 |
| 16 | +* sha512 |
| 17 | + |
| 18 | +Hashing an entire string at once can be done via the functions named |
| 19 | +after each hash: |
| 20 | + |
| 21 | +.. code-block:: dylan |
| 22 | +
|
| 23 | + let digest = sha1("Some text"); |
| 24 | +
|
| 25 | +If you want a printable digest, use :meth:`hexdigest(<byte-vector>)`: |
| 26 | + |
| 27 | +.. code-block:: dylan |
| 28 | +
|
| 29 | + let hexdigest = hexdigest(sha1("Some text")); |
| 30 | +
|
| 31 | +If you want to hash multiple strings into a single digest (useful when streaming), |
| 32 | +you can use the :gf:`update-hash` and :gf:`digest` functions: |
| 33 | + |
| 34 | +.. code-block:: dylan |
| 35 | +
|
| 36 | + let hash = make(<sha1>); |
| 37 | + update-hash(hash, "Some"); |
| 38 | + update-hash(hash, " "); |
| 39 | + update-hash(hash, "text"); |
| 40 | + let digest = digest(hash); |
| 41 | + // hexdigest works on hashes as well: |
| 42 | + let hexdigest = hexdigest(hash); |
| 43 | +
|
| 44 | +The hash-algorithms Module |
| 45 | +========================== |
| 46 | + |
| 47 | +.. class:: <hash> |
| 48 | + |
| 49 | + :superclasses: :drm:`<object>` |
| 50 | + |
| 51 | +.. generic-function:: digest-size |
| 52 | + |
| 53 | + Returns the digest size of the hash algorithm. |
| 54 | + |
| 55 | + :signature: digest-size (hash) => (digest-size) |
| 56 | + |
| 57 | + :parameter hash: An instance of :class:`<hash>`. |
| 58 | + :value digest-size: An instance of :drm:`<integer>`. |
| 59 | + |
| 60 | +.. generic-function:: block-size |
| 61 | + |
| 62 | + Returns the block size of the hash algorithm. |
| 63 | + |
| 64 | + :signature: block-size (hash) => (block-size) |
| 65 | + |
| 66 | + :parameter hash: An instance of :class:`<hash>`. |
| 67 | + :value block-size: An instance of :drm:`<integer>`. |
| 68 | + |
| 69 | +.. generic-function:: update-hash |
| 70 | + |
| 71 | + Add more data to the hash. |
| 72 | + |
| 73 | + :signature: update-hash (hash, input) => () |
| 74 | + |
| 75 | + :parameter hash: An instance of :class:`<hash>`. |
| 76 | + :parameter input: An instance of :drm:`<byte-string>`, :class:`<buffer>` or :class:`collections:byte-vector:<byte-vector>`. |
| 77 | + |
| 78 | + :description: |
| 79 | + |
| 80 | + Add more data to the hash. This is useful when streaming data or the data is |
| 81 | + available in multiple strings and you wish to avoid the overhead of concatenation. |
| 82 | + |
| 83 | + Calling ``update-hash`` multiple times is equivalent to calling it once with |
| 84 | + a concatenation of the arguments: |
| 85 | + |
| 86 | + .. code-block:: dylan |
| 87 | +
|
| 88 | + let hash-separate = make(<sha1>); |
| 89 | + update-hash(hash-separate, "Some"); |
| 90 | + update-hash(hash-separate, " "); |
| 91 | + update-hash(hash-separate, "text"); |
| 92 | + let digest-separate = digest(hash-separate); |
| 93 | +
|
| 94 | + let hash-combined = make(<sha1>); |
| 95 | + update-hash(hash-combined, "Some text"); |
| 96 | + let digest-combined = digest(hash-combined); |
| 97 | +
|
| 98 | + // digest-separate and digest-combined will be the same |
| 99 | +
|
| 100 | + :seealso: |
| 101 | + |
| 102 | + - :gf:`digest` |
| 103 | + - :meth:`hexdigest(<hash>)` |
| 104 | + - :meth:`hexdigest(<byte-vector>)` |
| 105 | + |
| 106 | +.. generic-function:: digest |
| 107 | + |
| 108 | + :signature: digest (hash) => (digest) |
| 109 | + |
| 110 | + :parameter hash: An instance of :class:`<hash>`. |
| 111 | + :value digest: An instance of :class:`collections:byte-vector:<byte-vector>`. |
| 112 | + |
| 113 | + :description: |
| 114 | + |
| 115 | + The return value *digest* is binary data and may include null bytes. To display |
| 116 | + this result in text form, use :meth:`hexdigest(<hash>)` or |
| 117 | + :meth:`hexdigest(<byte-vector>)`. |
| 118 | + |
| 119 | + Use :gf:`update-hash` to add data to the hash. |
| 120 | + |
| 121 | + :seealso: |
| 122 | + |
| 123 | + - :gf:`update-hash` |
| 124 | + - :meth:`hexdigest(<hash>)` |
| 125 | + - :meth:`hexdigest(<byte-vector>)` |
| 126 | + |
| 127 | +.. method:: hexdigest |
| 128 | + :specializer: <hash> |
| 129 | + |
| 130 | + Returns the digest for the given hash as a hexadecimal string. |
| 131 | + |
| 132 | + :signature: hexdigest (hash) => (hexdigest) |
| 133 | + |
| 134 | + :parameter hash: An instance of :class:`<hash>`. |
| 135 | + :value hexdigest: An instance of :drm:`<byte-string>`. |
| 136 | + |
| 137 | + :seealso: |
| 138 | + |
| 139 | + - :gf:`digest` |
| 140 | + - :meth:`hexdigest(<byte-vector>)` |
| 141 | + |
| 142 | +.. method:: hexdigest |
| 143 | + :specializer: <byte-vector> |
| 144 | + |
| 145 | + Returns the digest given as a hexadecimal string. |
| 146 | + |
| 147 | + :signature: hexdigest (digest) => (hexdigest) |
| 148 | + |
| 149 | + :parameter digest: An instance of :class:`collections:byte-vector:<byte-vector>`. |
| 150 | + :value hexdigest: An instance of :drm:`<byte-string>`. |
| 151 | + |
| 152 | + :seealso: |
| 153 | + |
| 154 | + - :gf:`digest` |
| 155 | + - :meth:`hexdigest(<hash>)` |
| 156 | + |
| 157 | +MD5 |
| 158 | +--- |
| 159 | + |
| 160 | +.. class:: <md5> |
| 161 | + |
| 162 | + :superclasses: :class:`<hash>` |
| 163 | + |
| 164 | +.. function:: md5 |
| 165 | + |
| 166 | + :signature: md5 (input) => (digest) |
| 167 | + |
| 168 | + :parameter input: An instance of :drm:`<byte-string>`, :class:`<buffer>` or :class:`collections:byte-vector:<byte-vector>`. |
| 169 | + :value digest: An instance of :class:`collections:byte-vector:<byte-vector>`. |
| 170 | + |
| 171 | +SHA-1 |
| 172 | +----- |
| 173 | + |
| 174 | +.. class:: <sha1> |
| 175 | + |
| 176 | + :superclasses: :class:`<hash>` |
| 177 | + |
| 178 | +.. function:: sha1 |
| 179 | + |
| 180 | + :signature: sha1 (input) => (digest) |
| 181 | + |
| 182 | + :parameter input: An instance of :drm:`<byte-string>`, :class:`<buffer>` or :class:`collections:byte-vector:<byte-vector>`. |
| 183 | + :value digest: An instance of :class:`collections:byte-vector:<byte-vector>`. |
| 184 | + |
| 185 | +SHA-2 |
| 186 | +----- |
| 187 | + |
| 188 | +.. class:: <sha256> |
| 189 | + |
| 190 | + :superclasses: :class:`<hash>` |
| 191 | + |
| 192 | +.. function:: sha256 |
| 193 | + |
| 194 | + :signature: sha256 (input) => (digest) |
| 195 | + |
| 196 | + :parameter input: An instance of :drm:`<byte-string>`, :class:`<buffer>` or :class:`collections:byte-vector:<byte-vector>`. |
| 197 | + :value digest: An instance of :class:`collections:byte-vector:<byte-vector>`. |
| 198 | + |
| 199 | +.. class:: <sha224> |
| 200 | + |
| 201 | + :superclasses: :class:`<hash>` |
| 202 | + |
| 203 | +.. function:: sha224 |
| 204 | + |
| 205 | + :signature: sha224 (input) => (digest) |
| 206 | + |
| 207 | + :parameter input: An instance of :drm:`<byte-string>`, :class:`<buffer>` or :class:`collections:byte-vector:<byte-vector>`. |
| 208 | + :value digest: An instance of :class:`collections:byte-vector:<byte-vector>`. |
| 209 | + |
| 210 | +.. class:: <sha384> |
| 211 | + |
| 212 | + :superclasses: :class:`<hash>` |
| 213 | + |
| 214 | +.. function:: sha384 |
| 215 | + |
| 216 | + :signature: sha384 (input) => (digest) |
| 217 | + |
| 218 | + :parameter input: An instance of :drm:`<byte-string>`, :class:`<buffer>` or :class:`collections:byte-vector:<byte-vector>`. |
| 219 | + :value digest: An instance of :class:`collections:byte-vector:<byte-vector>`. |
| 220 | + |
| 221 | +.. class:: <sha512> |
| 222 | + |
| 223 | + :superclasses: :class:`<hash>` |
| 224 | + |
| 225 | +.. function:: sha512 |
| 226 | + |
| 227 | + :signature: sha512 (input) => (digest) |
| 228 | + |
| 229 | + :parameter input: An instance of :drm:`<byte-string>`, :class:`<buffer>` or :class:`collections:byte-vector:<byte-vector>`. |
| 230 | + :value digest: An instance of :class:`collections:byte-vector:<byte-vector>`. |
0 commit comments