summary refs log tree commit diff stats
path: root/nimsuggest
Commit message (Expand)AuthorAgeFilesLines
* fix deprecations and other warnings (#13748)Miran2020-03-251-3/+3
* fix nimsuggest warning (#13699)Jasper Jenkins2020-03-203-7/+4
* fix .deprecated. object typedef crash (#13643)Andy Davidoff2020-03-162-8/+7
* Revert "nimsuggest: don't add CRLF to replies (#13545)" (#13597)alaviss2020-03-061-4/+4
* nimsuggest: don't add CRLF to replies (#13545)alaviss2020-03-041-4/+4
* make case-object transitions explicit, make unknownLineInfo a const, replace ...Jasper Jenkins2020-01-171-1/+0
* fix nimsuggest deprecation warnings (#12772)Jasper Jenkins2019-11-301-2/+2
* remove unused importsnarimiran2019-11-061-4/+3
* compiler/suggest: add variable support to `con` (#12569)alaviss2019-11-041-0/+12
* compiler/semtypes: improve lineinfo for exported object fields (#12495)alaviss2019-10-251-0/+11
* compiler/semcall: return the correct lineinfo for nkCallStrLit (#12484)alaviss2019-10-241-0/+11
* nimsuggest: add a command that returns the project file (#12411)alaviss2019-10-111-1/+10
* nimsuggest: add option to force finding the project file (#12409)alaviss2019-10-111-1/+10
* nimsuggest: fix tcp socket leak for epc backend (#12384) [backport]alaviss2019-10-081-1/+1
* nimsuggest: fix tcp socket leak (#12377) [backport]alaviss2019-10-081-1/+1
* Fix spellings (#12277) [backport]Federico Ceratto2019-09-272-2/+2
* fixes #12125 (#12131)Andreas Rumpf2019-09-061-0/+0
* minor style changesAraq2019-07-112-10/+10
* [bugfix] fix #11482, fix nimsuggest range error (#11519)Miran2019-06-171-2/+2
* [other] nimsuggest: adhere to our style guideAraq2019-06-131-2/+2
* make fullpaths the default in error messages and stack traces for mor… (#11...Andreas Rumpf2019-06-051-0/+1
* update nimsuggestAraq2019-05-271-39/+15
* Allow void macro result (#11286)Arne Döring2019-05-211-1/+1
* move assertions and iterators out of system.nim (#10597)Miran2019-03-072-3/+2
* nimsuggest: make use project file name if none given for 'sug' command; allow...Andreas Rumpf2019-03-041-4/+8
* StringStream and parseJson, parseCfg, parseSql et al for the vm (#10746)Arne Döring2019-02-281-30/+25
* better docs: osproc (#10708)Miran2019-02-192-3/+3
* document proper imports, fixes #8164 (#10698)Miran2019-02-191-0/+2
* compiler/types: correctly generates signature for non-proc types (#10658)alaviss2019-02-131-0/+12
* nimsuggest: only log when logging is enabled (#10646)alaviss2019-02-131-2/+2
* compiler/[msgs, suggest]: improve highlighter accuracy (#10496)alaviss2019-02-072-0/+21
* suggest: quote operators and keywords on suggestion (#10460)alaviss2019-01-281-0/+10
* semcall: correct lineinfo for accquoted symbols (#10461)alaviss2019-01-271-0/+2
* compiler/sem: correct macros lineinfo (#10438)alaviss2019-01-241-0/+13
* compiler/sem*: better lineinfo for templates (#10428)alaviss2019-01-233-2/+16
* nimsuggest/tester: disable highlight tests for epc (#10426)alaviss2019-01-233-2/+4
* compiler/sem*: improve lineinfo for qualified and generic procs (#10427)alaviss2019-01-232-0/+27
* Correct lineinfo for accent quoted symbols in proc definition (#10399)alaviss2019-01-211-0/+8
* nimsuggest: add an option to bind to a free port (#10328)alaviss2019-01-161-2/+15
* fixes nim-lang/nimsuggest#76 (#10093)alaviss2018-12-252-2/+30
* Moved some code around in nimsuggest to avoid crash when run as library (#9818)PMunch2018-12-011-8/+22
* Made nimsuggest importable as a library and add Nim-path override option (#9784)PMunch2018-11-231-1/+100
* nimsuggest: fixes #96Timothee Cour2018-11-201-2/+3
* refs #9637 ; cleanup installation of nimsuggest (#9703)Timothee Cour2018-11-151-1/+0
* remove ---path:"../compiler" from nimsuggest/nimsuggest.nim.cfgTimothee Cour2018-11-071-1/+0
* compiler refactoring; use typesafe path handing; docgen: render symbols betwe...Andreas Rumpf2018-09-071-18/+21
* make config.nims behave like nim.cfg in terms of where these scripts are sear...Timothee Cour2018-08-301-48/+25
* add nim c -r nimsuggest/tester to travis (#8805)Timothee Cour2018-08-299-2/+26
* make more tests greenAndreas Rumpf2018-08-131-3/+3
* cleanup nimsuggest tests a bitAndreas Rumpf2018-08-103-11/+11
revision' href='/ahoang/Nim/blame/lib/std/sha1.nim?h=devel&id=66c0f7c3fb214485ca6cfd799af6e50798fcdf6d'>^
87a2ced1b ^
18023c023 ^
87a2ced1b ^



18023c023 ^
87a2ced1b ^



18023c023 ^
87a2ced1b ^



18023c023 ^


87a2ced1b ^




18023c023 ^

87a2ced1b ^



18023c023 ^

87a2ced1b ^




18023c023 ^

87a2ced1b ^



18023c023 ^

87a2ced1b ^




18023c023 ^

87a2ced1b ^




18023c023 ^
d914dca51 ^
cee58ccf7 ^



18023c023 ^





























d914dca51 ^
cee58ccf7 ^



18023c023 ^
87a2ced1b ^
18023c023 ^














139497834 ^
cee58ccf7 ^
4a0447045 ^

cee58ccf7 ^

4a0447045 ^


cee58ccf7 ^
18023c023 ^




cee58ccf7 ^
4a0447045 ^

cee58ccf7 ^

baaa19b92 ^





ce7caec4b ^
baaa19b92 ^








18023c023 ^
274fafb2d ^
cee58ccf7 ^
4a0447045 ^

cee58ccf7 ^
4a0447045 ^


cee58ccf7 ^
274fafb2d ^




cee58ccf7 ^
4a0447045 ^

cee58ccf7 ^

4a0447045 ^




cee58ccf7 ^
274fafb2d ^



cee58ccf7 ^
4a0447045 ^






cee58ccf7 ^
274fafb2d ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284







                                                   
                                                  
  


                                                                         
  

              
  



                                                                  
                  


                                                                      
  

                         
  


                                                                          

                                                                                                  
 

                                                



                         
                                                     

                                   
    
                     



                           
                                                                                

                                                                    
                                 



                                                                     











                                                                  

                           

           




                  
 



                                                                     

               




                               
          
                                                 
 



                                                                     
 



                              
 



                                                           


               




                               

          



                                                                           

               




                               

          



                                                           

               




                               

          




                   
 
                                                         



                                                                     





























                                                                               
                                                



                                                                     
                                 
           














                                                                
                                                    
                                         

                  

                                                                                                       


                                                                              
 




                                                    
                                          

                  

                                                                                                       





                                      
                                     








                              
 
                                     
                                                         

                  
                                                                                                   


                                                              
 




                                                 
                                                

                  

                                                                                                   




                                                          
 



                                                                         
                                                     






                                                                     
 

                                                                         
#
#
#           The Nim Compiler
#        (c) Copyright 2015 Nim Contributors
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#
## **Note:** Import `std/sha1` to use this module.
##
## [SHA-1 (Secure Hash Algorithm 1)](https://en.wikipedia.org/wiki/SHA-1)
## is a cryptographic hash function which takes an input and produces
## a 160-bit (20-byte) hash value known as a message digest.
##
## Basic usage
## ===========
##
runnableExamples:
  let accessName = secureHash("John Doe")
  assert $accessName == "AE6E4D1209F17B460503904FAD297B31E9CF6362"

## .. code-block::
##   let
##     a = secureHashFile("myFile.nim")
##     b = parseSecureHash("10DFAEBF6BFDBC7939957068E2EFACEC4972933C")
##
##   if a == b:
##     echo "Files match"
##
## See also
## ========
## * `base64 module<base64.html>`_ implements a Base64 encoder and decoder
## * `hashes module<hashes.html>`_ for efficient computations of hash values for diverse Nim types
## * `md5 module<md5.html>`_ implements the MD5 checksum algorithm

import std/strutils
from std/endians import bigEndian32, bigEndian64

const Sha1DigestSize = 20

type
  Sha1Digest* = array[0 .. Sha1DigestSize - 1, uint8]
  SecureHash* = distinct Sha1Digest

type
  Sha1State* = object
    count: int
    state: array[5, uint32]
    buf:   array[64, byte]

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

proc newSha1State*(): Sha1State =
  ## Creates a `Sha1State`.
  ##
  ## If you use the `secureHash proc <#secureHash,openArray[char]>`_,
  ## there's no need to call this function explicitly.
  result.count = 0
  result.state[0] = 0x67452301'u32
  result.state[1] = 0xEFCDAB89'u32
  result.state[2] = 0x98BADCFE'u32
  result.state[3] = 0x10325476'u32
  result.state[4] = 0xC3D2E1F0'u32

template ror27(val: uint32): uint32 = (val shr 27) or (val shl  5)
template ror2 (val: uint32): uint32 = (val shr  2) or (val shl 30)
template ror31(val: uint32): uint32 = (val shr 31) or (val shl  1)

proc transform(ctx: var Sha1State) =
  var w: array[80, uint32]
  var a, b, c, d, e: uint32
  var t = 0

  a = ctx.state[0]
  b = ctx.state[1]
  c = ctx.state[2]
  d = ctx.state[3]
  e = ctx.state[4]

  template shaF1(a, b, c, d, e, t: untyped) =
    bigEndian32(addr w[t], addr ctx.buf[t * 4])
    e += ror27(a) + w[t] + (d xor (b and (c xor d))) + 0x5A827999'u32
    b = ror2(b)

  while t < 15:
    shaF1(a, b, c, d, e, t + 0)
    shaF1(e, a, b, c, d, t + 1)
    shaF1(d, e, a, b, c, t + 2)
    shaF1(c, d, e, a, b, t + 3)
    shaF1(b, c, d, e, a, t + 4)
    t += 5
  shaF1(a, b, c, d, e, t + 0) # 16th one, t == 15

  template shaF11(a, b, c, d, e, t: untyped) =
    w[t] = ror31(w[t-3] xor w[t-8] xor w[t-14] xor w[t-16])
    e += ror27(a) + w[t] + (d xor (b and (c xor d))) + 0x5A827999'u32
    b = ror2(b)

  shaF11(e, a, b, c, d, t + 1)
  shaF11(d, e, a, b, c, t + 2)
  shaF11(c, d, e, a, b, t + 3)
  shaF11(b, c, d, e, a, t + 4)

  template shaF2(a, b, c, d, e, t: untyped) =
    w[t] = ror31(w[t-3] xor w[t-8] xor w[t-14] xor w[t-16])
    e += ror27(a) + w[t] + (b xor c xor d) + 0x6ED9EBA1'u32
    b = ror2(b)

  t = 20
  while t < 40:
    shaF2(a, b, c, d, e, t + 0)
    shaF2(e, a, b, c, d, t + 1)
    shaF2(d, e, a, b, c, t + 2)
    shaF2(c, d, e, a, b, t + 3)
    shaF2(b, c, d, e, a, t + 4)
    t += 5

  template shaF3(a, b, c, d, e, t: untyped) =
    w[t] = ror31(w[t-3] xor w[t-8] xor w[t-14] xor w[t-16])
    e += ror27(a) + w[t] + ((b and c) or (d and (b or c))) + 0x8F1BBCDC'u32
    b = ror2(b)

  while t < 60:
    shaF3(a, b, c, d, e, t + 0)
    shaF3(e, a, b, c, d, t + 1)
    shaF3(d, e, a, b, c, t + 2)
    shaF3(c, d, e, a, b, t + 3)
    shaF3(b, c, d, e, a, t + 4)
    t += 5

  template shaF4(a, b, c, d, e, t: untyped) =
    w[t] = ror31(w[t-3] xor w[t-8] xor w[t-14] xor w[t-16])
    e += ror27(a) + w[t] + (b xor c xor d) + 0xCA62C1D6'u32
    b = ror2(b)

  while t < 80:
    shaF4(a, b, c, d, e, t + 0)
    shaF4(e, a, b, c, d, t + 1)
    shaF4(d, e, a, b, c, t + 2)
    shaF4(c, d, e, a, b, t + 3)
    shaF4(b, c, d, e, a, t + 4)
    t += 5

  ctx.state[0] += a
  ctx.state[1] += b
  ctx.state[2] += c
  ctx.state[3] += d
  ctx.state[4] += e

proc update*(ctx: var Sha1State, data: openArray[char]) =
  ## Updates the `Sha1State` with `data`.
  ##
  ## If you use the `secureHash proc <#secureHash,openArray[char]>`_,
  ## there's no need to call this function explicitly.
  var i = ctx.count mod 64
  var j = 0
  var len = data.len
  # Gather 64-bytes worth of data in order to perform a round with the leftover
  # data we had stored (but not processed yet)
  if len > 64 - i:
    copyMem(addr ctx.buf[i], unsafeAddr data[j], 64 - i)
    len -= 64 - i
    j += 64 - i
    transform(ctx)
    # Update the index since it's used in the while loop below _and_ we want to
    # keep its value if this code path isn't executed
    i = 0
  # Process the bulk of the payload
  while len >= 64:
    copyMem(addr ctx.buf[0], unsafeAddr data[j], 64)
    len -= 64
    j += 64
    transform(ctx)
  # Process the tail of the payload (len is < 64)
  while len > 0:
    dec len
    ctx.buf[i] = byte(data[j])
    inc i
    inc j
    if i == 64:
      transform(ctx)
      i = 0
  ctx.count += data.len

proc finalize*(ctx: var Sha1State): Sha1Digest =
  ## Finalizes the `Sha1State` and returns a `Sha1Digest`.
  ##
  ## If you use the `secureHash proc <#secureHash,openArray[char]>`_,
  ## there's no need to call this function explicitly.
  var cnt = uint64(ctx.count * 8)
  # a 1 bit
  update(ctx, "\x80")
  # Add padding until we reach a complexive size of 64 - 8 bytes
  while (ctx.count mod 64) != (64 - 8):
    update(ctx, "\x00")
  # The message length as a 64bit BE number completes the block
  var tmp: array[8, char]
  bigEndian64(addr tmp[0], addr cnt)
  update(ctx, tmp)
  # Turn the result into a single 160-bit number
  for i in 0 ..< 5:
    bigEndian32(addr ctx.state[i], addr ctx.state[i])
  copyMem(addr result[0], addr ctx.state[0], Sha1DigestSize)

# Public API

proc secureHash*(str: openArray[char]): SecureHash =
  ## Generates a `SecureHash` from `str`.
  ##
  ## **See also:**
  ## * `secureHashFile proc <#secureHashFile,string>`_ for generating a `SecureHash` from a file
  ## * `parseSecureHash proc <#parseSecureHash,string>`_ for converting a string `hash` to `SecureHash`
  runnableExamples:
    let hash = secureHash("Hello World")
    assert hash == parseSecureHash("0A4D55A8D778E5022FAB701977C5D840BBC486D0")

  var state = newSha1State()
  state.update(str)
  SecureHash(state.finalize())

proc secureHashFile*(filename: string): SecureHash =
  ## Generates a `SecureHash` from a file.
  ##
  ## **See also:**
  ## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a `SecureHash` from a string
  ## * `parseSecureHash proc <#parseSecureHash,string>`_ for converting a string `hash` to `SecureHash`
  const BufferLength = 8192

  let f = open(filename)
  var state = newSha1State()
  var buffer = newString(BufferLength)
  while true:
    let length = readChars(f, buffer)
    if length == 0:
      break
    buffer.setLen(length)
    state.update(buffer)
    if length != BufferLength:
      break
  close(f)

  SecureHash(state.finalize())

proc `$`*(self: SecureHash): string =
  ## Returns the string representation of a `SecureHash`.
  ##
  ## **See also:**
  ## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a `SecureHash` from a string
  runnableExamples:
    let hash = secureHash("Hello World")
    assert $hash == "0A4D55A8D778E5022FAB701977C5D840BBC486D0"

  result = ""
  for v in Sha1Digest(self):
    result.add(toHex(int(v), 2))

proc parseSecureHash*(hash: string): SecureHash =
  ## Converts a string `hash` to a `SecureHash`.
  ##
  ## **See also:**
  ## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a `SecureHash` from a string
  ## * `secureHashFile proc <#secureHashFile,string>`_ for generating a `SecureHash` from a file
  runnableExamples:
    let
      hashStr = "0A4D55A8D778E5022FAB701977C5D840BBC486D0"
      secureHash = secureHash("Hello World")
    assert secureHash == parseSecureHash(hashStr)

  for i in 0 ..< Sha1DigestSize:
    Sha1Digest(result)[i] = uint8(parseHexInt(hash[i*2] & hash[i*2 + 1]))

proc `==`*(a, b: SecureHash): bool =
  ## Checks if two `SecureHash` values are identical.
  runnableExamples:
    let
      a = secureHash("Hello World")
      b = secureHash("Goodbye World")
      c = parseSecureHash("0A4D55A8D778E5022FAB701977C5D840BBC486D0")
    assert a != b
    assert a == c

  # Not a constant-time comparison, but that's acceptable in this context
  Sha1Digest(a) == Sha1Digest(b)