Skip to content

Commit 21e1fa6

Browse files
konsumlammnarimiran
authored andcommitted
Improve documentation for std/sha1 (#16970)
(cherry picked from commit cee58cc)
1 parent bf06fb7 commit 21e1fa6

File tree

1 file changed

+50
-35
lines changed

1 file changed

+50
-35
lines changed

lib/std/sha1.nim

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,40 @@
66
# See the file "copying.txt", included in this
77
# distribution, for details about the copyright.
88
#
9-
10-
## **Note:** Import ``std/sha1`` to use this module
11-
##
12-
## SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function which
13-
## takes an input and produces a 160-bit (20-byte) hash value known as a
14-
## message digest.
9+
## **Note:** Import `std/sha1` to use this module.
1510
##
16-
## .. code-block::
17-
## import std/sha1
11+
## [SHA-1 (Secure Hash Algorithm 1)](https://en.wikipedia.org/wiki/SHA-1)
12+
## is a cryptographic hash function which takes an input and produces
13+
## a 160-bit (20-byte) hash value known as a message digest.
1814
##
19-
## let accessName = secureHash("John Doe")
20-
## assert $accessName == "AE6E4D1209F17B460503904FAD297B31E9CF6362"
15+
## Basic usage
16+
## ===========
2117
##
18+
runnableExamples:
19+
let accessName = secureHash("John Doe")
20+
assert $accessName == "AE6E4D1209F17B460503904FAD297B31E9CF6362"
21+
2222
## .. code-block::
23-
## import std/sha1
24-
##
25-
## let
26-
## a = secureHashFile("myFile.nim")
27-
## b = parseSecureHash("10DFAEBF6BFDBC7939957068E2EFACEC4972933C")
23+
## let
24+
## a = secureHashFile("myFile.nim")
25+
## b = parseSecureHash("10DFAEBF6BFDBC7939957068E2EFACEC4972933C")
2826
##
29-
## if a == b:
30-
## echo "Files match"
27+
## if a == b:
28+
## echo "Files match"
3129
##
32-
## **See also:**
33-
## * `base64 module<base64.html>`_ implements a base64 encoder and decoder
30+
## See also
31+
## ========
32+
## * `base64 module<base64.html>`_ implements a Base64 encoder and decoder
3433
## * `hashes module<hashes.html>`_ for efficient computations of hash values for diverse Nim types
3534
## * `md5 module<md5.html>`_ implements the MD5 checksum algorithm
3635

37-
import strutils
38-
from endians import bigEndian32, bigEndian64
36+
import std/strutils
37+
from std/endians import bigEndian32, bigEndian64
3938

4039
const Sha1DigestSize = 20
4140

4241
type
43-
Sha1Digest* = array[0 .. Sha1DigestSize-1, uint8]
42+
Sha1Digest* = array[0 .. Sha1DigestSize - 1, uint8]
4443
SecureHash* = distinct Sha1Digest
4544

4645
type
@@ -49,10 +48,14 @@ type
4948
state: array[5, uint32]
5049
buf: array[64, byte]
5150

52-
# This implementation of the SHA1 algorithm was ported from the Chromium OS one
51+
# This implementation of the SHA-1 algorithm was ported from the Chromium OS one
5352
# with minor modifications that should not affect its functionality.
5453

5554
proc newSha1State*(): Sha1State =
55+
## Creates a `Sha1State`.
56+
##
57+
## If you use the `secureHash proc <#secureHash,openArray[char]>`_,
58+
## there's no need to call this function explicitly.
5659
result.count = 0
5760
result.state[0] = 0x67452301'u32
5861
result.state[1] = 0xEFCDAB89'u32
@@ -146,6 +149,10 @@ proc transform(ctx: var Sha1State) =
146149
ctx.state[4] += e
147150

148151
proc update*(ctx: var Sha1State, data: openArray[char]) =
152+
## Updates the `Sha1State` with `data`.
153+
##
154+
## If you use the `secureHash proc <#secureHash,openArray[char]>`_,
155+
## there's no need to call this function explicitly.
149156
var i = ctx.count mod 64
150157
var j = 0
151158
var len = data.len
@@ -177,6 +184,10 @@ proc update*(ctx: var Sha1State, data: openArray[char]) =
177184
ctx.count += data.len
178185

179186
proc finalize*(ctx: var Sha1State): Sha1Digest =
187+
## Finalizes the `Sha1State` and returns a `Sha1Digest`.
188+
##
189+
## If you use the `secureHash proc <#secureHash,openArray[char]>`_,
190+
## there's no need to call this function explicitly.
180191
var cnt = uint64(ctx.count * 8)
181192
# a 1 bit
182193
update(ctx, "\x80")
@@ -195,24 +206,25 @@ proc finalize*(ctx: var Sha1State): Sha1Digest =
195206
# Public API
196207

197208
proc secureHash*(str: openArray[char]): SecureHash =
198-
## Generates a ``SecureHash`` from a ``str``.
209+
## Generates a `SecureHash` from `str`.
199210
##
200211
## **See also:**
201-
## * `secureHashFile proc <#secureHashFile,string>`_ for generating a ``SecureHash`` from a file
202-
## * `parseSecureHash proc <#parseSecureHash,string>`_ for converting a string ``hash`` to ``SecureHash``
212+
## * `secureHashFile proc <#secureHashFile,string>`_ for generating a `SecureHash` from a file
213+
## * `parseSecureHash proc <#parseSecureHash,string>`_ for converting a string `hash` to `SecureHash`
203214
runnableExamples:
204215
let hash = secureHash("Hello World")
205216
assert hash == parseSecureHash("0A4D55A8D778E5022FAB701977C5D840BBC486D0")
217+
206218
var state = newSha1State()
207219
state.update(str)
208220
SecureHash(state.finalize())
209221

210222
proc secureHashFile*(filename: string): SecureHash =
211-
## Generates a ``SecureHash`` from a file.
223+
## Generates a `SecureHash` from a file.
212224
##
213225
## **See also:**
214-
## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a ``SecureHash`` from a string
215-
## * `parseSecureHash proc <#parseSecureHash,string>`_ for converting a string ``hash`` to ``SecureHash``
226+
## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a `SecureHash` from a string
227+
## * `parseSecureHash proc <#parseSecureHash,string>`_ for converting a string `hash` to `SecureHash`
216228
const BufferLength = 8192
217229

218230
let f = open(filename)
@@ -231,39 +243,42 @@ proc secureHashFile*(filename: string): SecureHash =
231243
SecureHash(state.finalize())
232244

233245
proc `$`*(self: SecureHash): string =
234-
## Returns the string representation of a ``SecureHash``.
246+
## Returns the string representation of a `SecureHash`.
235247
##
236248
## **See also:**
237-
## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a ``SecureHash`` from a string
249+
## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a `SecureHash` from a string
238250
runnableExamples:
239251
let hash = secureHash("Hello World")
240252
assert $hash == "0A4D55A8D778E5022FAB701977C5D840BBC486D0"
253+
241254
result = ""
242255
for v in Sha1Digest(self):
243256
result.add(toHex(int(v), 2))
244257

245258
proc parseSecureHash*(hash: string): SecureHash =
246-
## Converts a string ``hash`` to ``SecureHash``.
259+
## Converts a string `hash` to a `SecureHash`.
247260
##
248261
## **See also:**
249-
## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a ``SecureHash`` from a string
250-
## * `secureHashFile proc <#secureHashFile,string>`_ for generating a ``SecureHash`` from a file
262+
## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a `SecureHash` from a string
263+
## * `secureHashFile proc <#secureHashFile,string>`_ for generating a `SecureHash` from a file
251264
runnableExamples:
252265
let
253266
hashStr = "0A4D55A8D778E5022FAB701977C5D840BBC486D0"
254267
secureHash = secureHash("Hello World")
255268
assert secureHash == parseSecureHash(hashStr)
269+
256270
for i in 0 ..< Sha1DigestSize:
257271
Sha1Digest(result)[i] = uint8(parseHexInt(hash[i*2] & hash[i*2 + 1]))
258272

259273
proc `==`*(a, b: SecureHash): bool =
260-
## Checks if two ``SecureHash`` values are identical.
274+
## Checks if two `SecureHash` values are identical.
261275
runnableExamples:
262276
let
263277
a = secureHash("Hello World")
264278
b = secureHash("Goodbye World")
265279
c = parseSecureHash("0A4D55A8D778E5022FAB701977C5D840BBC486D0")
266280
assert a != b
267281
assert a == c
282+
268283
# Not a constant-time comparison, but that's acceptable in this context
269284
Sha1Digest(a) == Sha1Digest(b)

0 commit comments

Comments
 (0)