Merge pull request #2893 from nanoant/patch/securehash-fixup
SecureHash fixup
This commit is contained in:
commit
c237473e71
8 changed files with 26 additions and 32 deletions
|
|
@ -10,7 +10,7 @@
|
||||||
# abstract syntax tree + symbol table
|
# abstract syntax tree + symbol table
|
||||||
|
|
||||||
import
|
import
|
||||||
msgs, hashes, nversion, options, strutils, secure_hash, ropes, idents, lists,
|
msgs, hashes, nversion, options, strutils, securehash, ropes, idents, lists,
|
||||||
intsets, idgen
|
intsets, idgen
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
import
|
import
|
||||||
ast, astalgo, hashes, trees, platform, magicsys, extccomp, options, intsets,
|
ast, astalgo, hashes, trees, platform, magicsys, extccomp, options, intsets,
|
||||||
nversion, nimsets, msgs, secure_hash, bitsets, idents, lists, types,
|
nversion, nimsets, msgs, securehash, bitsets, idents, lists, types,
|
||||||
ccgutils, os, ropes, math, passes, rodread, wordrecg, treetab, cgmeth,
|
ccgutils, os, ropes, math, passes, rodread, wordrecg, treetab, cgmeth,
|
||||||
condsyms, rodutils, renderer, idgen, cgendata, ccgmerge, semfold, aliases,
|
condsyms, rodutils, renderer, idgen, cgendata, ccgmerge, semfold, aliases,
|
||||||
lowerings, semparallel
|
lowerings, semparallel
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,8 @@
|
||||||
# nim files.
|
# nim files.
|
||||||
|
|
||||||
import
|
import
|
||||||
lists, ropes, os, strutils, osproc, platform, condsyms, options, msgs, secure_hash
|
lists, ropes, os, strutils, osproc, platform, condsyms, options, msgs,
|
||||||
|
securehash
|
||||||
|
|
||||||
type
|
type
|
||||||
TSystemCC* = enum
|
TSystemCC* = enum
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ implements the required case distinction.
|
||||||
|
|
||||||
import
|
import
|
||||||
ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp, options,
|
ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp, options,
|
||||||
nversion, nimsets, msgs, secure_hash, bitsets, idents, lists, types, os,
|
nversion, nimsets, msgs, securehash, bitsets, idents, lists, types, os,
|
||||||
times, ropes, math, passes, ccgutils, wordrecg, renderer, rodread, rodutils,
|
times, ropes, math, passes, ccgutils, wordrecg, renderer, rodread, rodutils,
|
||||||
intsets, cgmeth, lowerings
|
intsets, cgmeth, lowerings
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
## implements the module handling
|
## implements the module handling
|
||||||
|
|
||||||
import
|
import
|
||||||
ast, astalgo, magicsys, secure_hash, rodread, msgs, cgendata, sigmatch, options,
|
ast, astalgo, magicsys, securehash, rodread, msgs, cgendata, sigmatch, options,
|
||||||
idents, os, lexer, idgen, passes, syntaxes, llstream
|
idents, os, lexer, idgen, passes, syntaxes, llstream
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@
|
||||||
|
|
||||||
import
|
import
|
||||||
os, options, strutils, nversion, ast, astalgo, msgs, platform, condsyms,
|
os, options, strutils, nversion, ast, astalgo, msgs, platform, condsyms,
|
||||||
ropes, idents, secure_hash, idgen, types, rodutils, memfiles
|
ropes, idents, securehash, idgen, types, rodutils, memfiles
|
||||||
|
|
||||||
type
|
type
|
||||||
TReasonForRecompile* = enum ## all the reasons that can trigger recompilation
|
TReasonForRecompile* = enum ## all the reasons that can trigger recompilation
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,8 @@
|
||||||
|
|
||||||
import
|
import
|
||||||
intsets, os, options, strutils, nversion, ast, astalgo, msgs, platform,
|
intsets, os, options, strutils, nversion, ast, astalgo, msgs, platform,
|
||||||
condsyms, ropes, idents, secure_hash, rodread, passes, importer, idgen, rodutils
|
condsyms, ropes, idents, securehash, rodread, passes, importer, idgen,
|
||||||
|
rodutils
|
||||||
|
|
||||||
# implementation
|
# implementation
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,36 +10,28 @@
|
||||||
import
|
import
|
||||||
strutils, unsigned
|
strutils, unsigned
|
||||||
|
|
||||||
const sha_digest_size = 20
|
const Sha1DigestSize = 20
|
||||||
|
|
||||||
type
|
type
|
||||||
SHA1Digest = array[0 .. sha_digest_size-1, uint8]
|
Sha1Digest = array[0 .. Sha1DigestSize-1, uint8]
|
||||||
SecureHash* = distinct SHA1Digest
|
SecureHash* = distinct Sha1Digest
|
||||||
|
|
||||||
const emptySecureHash = SecureHash([
|
proc sha1(src: string) : Sha1Digest
|
||||||
0u8, 0, 0, 0,
|
|
||||||
0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0,
|
|
||||||
])
|
|
||||||
|
|
||||||
proc sha1(src: string) : SHA1Digest
|
|
||||||
|
|
||||||
proc secureHash*(str: string): SecureHash = SecureHash(sha1(str))
|
proc secureHash*(str: string): SecureHash = SecureHash(sha1(str))
|
||||||
proc secureHashFile*(filename: string): SecureHash = secureHash(readFile(filename))
|
proc secureHashFile*(filename: string): SecureHash = secureHash(readFile(filename))
|
||||||
proc `$`*(self: SecureHash): string =
|
proc `$`*(self: SecureHash): string =
|
||||||
result = ""
|
result = ""
|
||||||
for v in SHA1Digest(self):
|
for v in Sha1Digest(self):
|
||||||
result.add(toHex(int(v), 2))
|
result.add(toHex(int(v), 2))
|
||||||
|
|
||||||
proc parseSecureHash*(hash: string): SecureHash =
|
proc parseSecureHash*(hash: string): SecureHash =
|
||||||
for i in 0.. <sha_digest_size:
|
for i in 0.. <Sha1DigestSize:
|
||||||
SHA1Digest(result)[i] = uint8(parseHexInt(hash[i*2] & hash[i*2 + 1]))
|
Sha1Digest(result)[i] = uint8(parseHexInt(hash[i*2] & hash[i*2 + 1]))
|
||||||
|
|
||||||
proc `==`*(a, b: SecureHash): bool =
|
proc `==`*(a, b: SecureHash): bool =
|
||||||
# Not a constant-time comparison, but that's acceptable in this context
|
# Not a constant-time comparison, but that's acceptable in this context
|
||||||
SHA1Digest(a) == SHA1Digest(b)
|
Sha1Digest(a) == Sha1Digest(b)
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
@ -76,20 +68,20 @@ when isMainModule:
|
||||||
# Ported to Nim by Erik O'Leary
|
# Ported to Nim by Erik O'Leary
|
||||||
|
|
||||||
type
|
type
|
||||||
SHA1State = array[0 .. 5-1, uint32]
|
Sha1State = array[0 .. 5-1, uint32]
|
||||||
SHA1Buffer = array[0 .. 80-1, uint32]
|
Sha1Buffer = array[0 .. 80-1, uint32]
|
||||||
|
|
||||||
template clearBuffer(w: SHA1Buffer, len = 16) =
|
template clearBuffer(w: Sha1Buffer, len = 16) =
|
||||||
zeroMem(addr(w), len * sizeof(uint32))
|
zeroMem(addr(w), len * sizeof(uint32))
|
||||||
|
|
||||||
proc init(result: var SHA1State) =
|
proc init(result: var Sha1State) =
|
||||||
result[0] = 0x67452301'u32
|
result[0] = 0x67452301'u32
|
||||||
result[1] = 0xefcdab89'u32
|
result[1] = 0xefcdab89'u32
|
||||||
result[2] = 0x98badcfe'u32
|
result[2] = 0x98badcfe'u32
|
||||||
result[3] = 0x10325476'u32
|
result[3] = 0x10325476'u32
|
||||||
result[4] = 0xc3d2e1f0'u32
|
result[4] = 0xc3d2e1f0'u32
|
||||||
|
|
||||||
proc innerHash(state: var SHA1State, w: var SHA1Buffer) =
|
proc innerHash(state: var Sha1State, w: var Sha1Buffer) =
|
||||||
var
|
var
|
||||||
a = state[0]
|
a = state[0]
|
||||||
b = state[1]
|
b = state[1]
|
||||||
|
|
@ -147,11 +139,11 @@ proc innerHash(state: var SHA1State, w: var SHA1Buffer) =
|
||||||
|
|
||||||
template computeInternal(src: expr): stmt {.immediate.} =
|
template computeInternal(src: expr): stmt {.immediate.} =
|
||||||
#Initialize state
|
#Initialize state
|
||||||
var state: SHA1State
|
var state: Sha1State
|
||||||
init(state)
|
init(state)
|
||||||
|
|
||||||
#Create w buffer
|
#Create w buffer
|
||||||
var w: SHA1Buffer
|
var w: Sha1Buffer
|
||||||
|
|
||||||
#Loop through all complete 64byte blocks.
|
#Loop through all complete 64byte blocks.
|
||||||
let byteLen = src.len
|
let byteLen = src.len
|
||||||
|
|
@ -199,9 +191,9 @@ template computeInternal(src: expr): stmt {.immediate.} =
|
||||||
|
|
||||||
# Store hash in result pointer, and make sure we get in in the correct order
|
# Store hash in result pointer, and make sure we get in in the correct order
|
||||||
# on both endian models.
|
# on both endian models.
|
||||||
for i in 0 .. sha_digest_size-1:
|
for i in 0 .. Sha1DigestSize-1:
|
||||||
result[i] = uint8((int(state[i shr 2]) shr ((3-(i and 3)) * 8)) and 255)
|
result[i] = uint8((int(state[i shr 2]) shr ((3-(i and 3)) * 8)) and 255)
|
||||||
|
|
||||||
proc sha1(src: string) : SHA1Digest =
|
proc sha1(src: string) : Sha1Digest =
|
||||||
## Calculate SHA1 from input string
|
## Calculate SHA1 from input string
|
||||||
computeInternal(src)
|
computeInternal(src)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue