• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alerque / luacheck / 10600181429

28 Aug 2024 03:54PM UTC coverage: 97.063% (-0.01%) from 97.077%
10600181429

push

github

alerque
chore: Correct availability of clobals in reader/writers

3 of 3 new or added lines in 1 file covered. (100.0%)

5 existing lines in 4 files now uncovered.

6312 of 6503 relevant lines covered (97.06%)

23960.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.58
/src/luacheck/vendor/sha1/init.lua
1
local common = require "luacheck.vendor.sha1.common"
508✔
2

3
local sha1 = {
508✔
4
   -- Meta fields retained for compatibility.
5
   _VERSION     = "sha.lua 0.6.0",
508✔
6
   _URL         = "https://github.com/mpeterv/sha1",
508✔
7
   _DESCRIPTION = [[
8
SHA-1 secure hash and HMAC-SHA1 signature computation in Lua,
9
using bit and bit32 modules and Lua 5.3 operators when available
10
and falling back to a pure Lua implementation on Lua 5.1.
11
Based on code originally by Jeffrey Friedl and modified by
12
Eike Decker and Enrique García Cota.]],
508✔
13
   _LICENSE = [[
14
MIT LICENSE
15

16
Copyright (c) 2013 Enrique García Cota, Eike Decker, Jeffrey Friedl
17
Copyright (c) 2018 Peter Melnichenko
18

19
Permission is hereby granted, free of charge, to any person obtaining a
20
copy of this software and associated documentation files (the
21
"Software"), to deal in the Software without restriction, including
22
without limitation the rights to use, copy, modify, merge, publish,
23
distribute, sublicense, and/or sell copies of the Software, and to
24
permit persons to whom the Software is furnished to do so, subject to
25
the following conditions:
26

27
The above copyright notice and this permission notice shall be included
28
in all copies or substantial portions of the Software.
29

30
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
34
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
35
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
36
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.]]
508✔
37
}
38

39
sha1.version = "0.6.0"
508✔
40

41
local function choose_ops()
42
   if _VERSION:find("5%.3") then
508✔
UNCOV
43
      return "lua53_ops"
127✔
44
   elseif pcall(require, "bit") then
381✔
45
      return "bit_ops"
×
46
   elseif pcall(require, "bit32") then
381✔
47
      return "bit32_ops"
127✔
48
   else
49
      return "pure_lua_ops"
254✔
50
   end
51
end
52

53
local ops = require("luacheck.vendor.sha1." .. choose_ops())
508✔
54
local uint32_lrot = ops.uint32_lrot
508✔
55
local byte_xor = ops.byte_xor
508✔
56
local uint32_xor_3 = ops.uint32_xor_3
508✔
57
local uint32_xor_4 = ops.uint32_xor_4
508✔
58
local uint32_ternary = ops.uint32_ternary
508✔
59
local uint32_majority = ops.uint32_majority
508✔
60

61
local bytes_to_uint32 = common.bytes_to_uint32
508✔
62
local uint32_to_bytes = common.uint32_to_bytes
508✔
63

64
local sbyte = string.byte
508✔
65
local schar = string.char
508✔
66
local sformat = string.format
508✔
67
local srep = string.rep
508✔
68

69
local function hex_to_binary(hex)
70
   return (hex:gsub("..", function(hexval)
×
71
      return schar(tonumber(hexval, 16))
×
72
   end))
73
end
74

75
-- Calculates SHA1 for a string, returns it encoded as 40 hexadecimal digits.
76
function sha1.sha1(str)
508✔
77
   -- Input preprocessing.
78
   -- First, append a `1` bit and seven `0` bits.
79
   local first_append = schar(0x80)
32✔
80

81
   -- Next, append some zero bytes to make the length of the final message a multiple of 64.
82
   -- Eight more bytes will be added next.
83
   local non_zero_message_bytes = #str + 1 + 8
32✔
84
   local second_append = srep(schar(0), -non_zero_message_bytes % 64)
32✔
85

86
   -- Finally, append the length of the original message in bits as a 64-bit number.
87
   -- Assume that it fits into the lower 32 bits.
88
   local third_append = schar(0, 0, 0, 0, uint32_to_bytes(#str * 8))
32✔
89

90
   str = str .. first_append .. second_append .. third_append
32✔
91
   assert(#str % 64 == 0)
32✔
92

93
   -- Initialize hash value.
94
   local h0 = 0x67452301
32✔
95
   local h1 = 0xEFCDAB89
32✔
96
   local h2 = 0x98BADCFE
32✔
97
   local h3 = 0x10325476
32✔
98
   local h4 = 0xC3D2E1F0
32✔
99

100
   local w = {}
32✔
101

102
   -- Process the input in successive 64-byte chunks.
103
   for chunk_start = 1, #str, 64 do
96✔
104
      -- Load the chunk into W[0..15] as uint32 numbers.
105
      local uint32_start = chunk_start
64✔
106

107
      for i = 0, 15 do
1,088✔
108
         w[i] = bytes_to_uint32(sbyte(str, uint32_start, uint32_start + 3))
1,024✔
109
         uint32_start = uint32_start + 4
1,024✔
110
      end
111

112
      -- Extend the input vector.
113
      for i = 16, 79 do
4,160✔
114
         w[i] = uint32_lrot(uint32_xor_4(w[i - 3], w[i - 8], w[i - 14], w[i - 16]), 1)
4,096✔
115
      end
116

117
      -- Initialize hash value for this chunk.
118
      local a = h0
64✔
119
      local b = h1
64✔
120
      local c = h2
64✔
121
      local d = h3
64✔
122
      local e = h4
64✔
123

124
      -- Main loop.
125
      for i = 0, 79 do
5,184✔
126
         local f
127
         local k
128

129
         if i <= 19 then
5,120✔
130
            f = uint32_ternary(b, c, d)
1,280✔
131
            k = 0x5A827999
1,280✔
132
         elseif i <= 39 then
3,840✔
133
            f = uint32_xor_3(b, c, d)
1,280✔
134
            k = 0x6ED9EBA1
1,280✔
135
         elseif i <= 59 then
2,560✔
136
            f = uint32_majority(b, c, d)
1,280✔
137
            k = 0x8F1BBCDC
1,280✔
138
         else
139
            f = uint32_xor_3(b, c, d)
1,280✔
140
            k = 0xCA62C1D6
1,280✔
141
         end
142

143
         local temp = (uint32_lrot(a, 5) + f + e + k + w[i]) % 0x100000000
5,120✔
144
         e = d
5,120✔
145
         d = c
5,120✔
146
         c = uint32_lrot(b, 30)
5,120✔
147
         b = a
5,120✔
148
         a = temp
5,120✔
149
      end
150

151
      -- Add this chunk's hash to result so far.
152
      h0 = (h0 + a) % 0x100000000
64✔
153
      h1 = (h1 + b) % 0x100000000
64✔
154
      h2 = (h2 + c) % 0x100000000
64✔
155
      h3 = (h3 + d) % 0x100000000
64✔
156
      h4 = (h4 + e) % 0x100000000
64✔
157
   end
158

159
   return sformat("%08x%08x%08x%08x%08x", h0, h1, h2, h3, h4)
32✔
160
end
161

162
function sha1.binary(str)
508✔
163
   return hex_to_binary(sha1.sha1(str))
×
164
end
165

166
-- Precalculate replacement tables.
167
local xor_with_0x5c = {}
508✔
168
local xor_with_0x36 = {}
508✔
169

170
for i = 0, 0xff do
130,556✔
171
   xor_with_0x5c[schar(i)] = schar(byte_xor(0x5c, i))
130,048✔
172
   xor_with_0x36[schar(i)] = schar(byte_xor(0x36, i))
130,048✔
173
end
174

175
-- 512 bits.
176
local BLOCK_SIZE = 64
508✔
177

178
function sha1.hmac(key, text)
508✔
179
   if #key > BLOCK_SIZE then
×
180
      key = sha1.binary(key)
×
181
   end
182

183
   local key_xord_with_0x36 = key:gsub('.', xor_with_0x36) .. srep(schar(0x36), BLOCK_SIZE - #key)
×
184
   local key_xord_with_0x5c = key:gsub('.', xor_with_0x5c) .. srep(schar(0x5c), BLOCK_SIZE - #key)
×
185

186
   return sha1.sha1(key_xord_with_0x5c .. sha1.binary(key_xord_with_0x36 .. text))
×
187
end
188

189
function sha1.hmac_binary(key, text)
508✔
190
   return hex_to_binary(sha1.hmac(key, text))
×
191
end
192

193
setmetatable(sha1, {__call = function(_, str) return sha1.sha1(str) end})
508✔
194

195
return sha1
508✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc