Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use openarray of bytes in md5 #19307

Merged
merged 8 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
filename argument for more informative errors.
- Module `colors` expanded with missing colors from the CSS color standard.
- Fixed `lists.SinglyLinkedList` being broken after removing the last node ([#19353](https://github.com/nim-lang/Nim/pull/19353)).
- `md5` now works at compile time and in JavaScript.

## `std/smtp`

Expand Down
145 changes: 111 additions & 34 deletions lib/pure/md5.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Module for computing [MD5 checksums](https://en.wikipedia.org/wiki/MD5).
##
## **Note:** The procs in this module can be used at compile time.
## This module also works at compile time and in JavaScript.
##
## See also
## ========
Expand All @@ -34,15 +34,16 @@ type
buffer: MD5Buffer

const
padding: cstring = "\x80\0\0\0" &
"\0\0\0\0\0\0\0\0" &
"\0\0\0\0\0\0\0\0" &
"\0\0\0\0\0\0\0\0" &
"\0\0\0\0\0\0\0\0" &
"\0\0\0\0\0\0\0\0" &
"\0\0\0\0\0\0\0\0" &
"\0\0\0\0\0\0\0\0" &
"\0\0\0\0"
padding: array[0..63, uint8] = [
0x80'u8, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the best way to format this is


proc F(x, y, z: uint32): uint32 {.inline.} =
result = (x and y) or ((not x) and z)
Expand Down Expand Up @@ -79,7 +80,7 @@ proc II(a: var uint32, b, c, d, x: uint32, s: uint8, ac: uint32) =
rot(a, s)
a = a + b

proc encode(dest: var MD5Block, src: cstring) =
proc encode(dest: var MD5Block, src: openArray[uint8]) =
var j = 0
for i in 0..high(dest):
dest[i] = uint32(ord(src[j])) or
Expand All @@ -97,10 +98,62 @@ proc decode(dest: var openArray[uint8], src: openArray[uint32]) =
dest[i+3] = uint8(src[j] shr 24 and 0xff'u32)
inc(i, 4)

proc transform(buffer: pointer, state: var MD5State) =
const openArrayTest = compiles do:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really hate how these end up looking. Don't know if people think it's worth it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not good, we need to do something. The compiler could use its own version of md5.nim so that bootstrapping keeps working (the compiler doesn't need md5.nim at its own compile-time either) and then md5.nim can be written in the one good way that is supported with Nim devel / 1.6.

# unreliable check for nim >= 1.4
proc foo(x {.noalias.}: pointer) = discard
# this is what we actually want to check but it only errors during codegen:
discard [1, 2].toOpenArray(0, 1)
# this error was removed in #15414 which was only included 1.4 onward

when not openArrayTest:
template slice(s: string | cstring, a, b): openArray[uint8] =
s.toOpenArrayByte(a, b)

template slice(s: openArray[uint8], a, b): openArray[uint8] =
s.toOpenArray(a, b)
else:
template slice(s: string, a, b): openArray[uint8] =
when nimvm:
# toOpenArray is not implemented in VM
var s2 = newSeq[uint8](s.len)
for i in 0 ..< s2.len:
s2[i] = uint8(s[i])
s2
else:
s.toOpenArrayByte(a, b)

template slice(s: cstring, a, b): openArray[uint8] =
when nimvm:
# toOpenArray is not implemented in VM
slice($s, a, b)
else:
when defined(js):
# toOpenArrayByte for cstring is not implemented in JS
slice($s, a, b)
else:
s.toOpenArrayByte(a, b)

template slice(s: openArray[uint8], a, b): openArray[uint8] =
when nimvm:
s[a .. b]
else:
s.toOpenArray(a, b)

const useMem = declared(copyMem)

template memOrNot(withMem, withoutMem): untyped =
when nimvm:
withoutMem
else:
when useMem:
withMem
else:
withoutMem

proc transform(buffer: openArray[uint8], state: var MD5State) =
var
myBlock: MD5Block
encode(myBlock, cast[cstring](buffer))
encode(myBlock, buffer)
var a = state[0]
var b = state[1]
var c = state[2]
Expand Down Expand Up @@ -175,10 +228,18 @@ proc transform(buffer: pointer, state: var MD5State) =
state[3] = state[3] + d

proc md5Init*(c: var MD5Context) {.raises: [], tags: [], gcsafe.}
proc md5Update*(c: var MD5Context, input: cstring, len: int) {.raises: [],
proc md5Update*(c: var MD5Context, input: openArray[uint8]) {.raises: [],
tags: [], gcsafe.}
proc md5Final*(c: var MD5Context, digest: var MD5Digest) {.raises: [], tags: [], gcsafe.}

proc md5Update*(c: var MD5Context, input: cstring, len: int) {.raises: [],
tags: [], gcsafe.} =
## Updates the `MD5Context` with the `input` data of length `len`.
##
## If you use the `toMD5 proc <#toMD5,string>`_, there's no need to call this
## function explicitly.
md5Update(c, input.slice(0, len - 1))


proc toMD5*(s: string): MD5Digest =
## Computes the `MD5Digest` value for a string `s`.
Expand All @@ -192,7 +253,7 @@ proc toMD5*(s: string): MD5Digest =

var c: MD5Context
md5Init(c)
md5Update(c, cstring(s), len(s))
md5Update(c, s.slice(0, s.len - 1))
md5Final(c, result)

proc `$`*(d: MD5Digest): string =
Expand All @@ -215,7 +276,7 @@ proc getMD5*(s: string): string =
c: MD5Context
d: MD5Digest
md5Init(c)
md5Update(c, cstring(s), len(s))
md5Update(c, s.slice(0, s.len - 1))
md5Final(c, d)
result = $d

Expand All @@ -226,6 +287,12 @@ proc `==`*(D1, D2: MD5Digest): bool =
return true


proc clearBuffer(c: var MD5Context) {.inline.} =
memOrNot:
zeroMem(addr(c.buffer), sizeof(MD5Buffer))
do:
reset(c.buffer)

proc md5Init*(c: var MD5Context) =
## Initializes an `MD5Context`.
##
Expand All @@ -237,29 +304,39 @@ proc md5Init*(c: var MD5Context) =
c.state[3] = 0x10325476'u32
c.count[0] = 0'u32
c.count[1] = 0'u32
zeroMem(addr(c.buffer), sizeof(MD5Buffer))
clearBuffer(c)

proc md5Update*(c: var MD5Context, input: cstring, len: int) =
## Updates the `MD5Context` with the `input` data of length `len`.
proc writeBuffer(c: var MD5Context, index: int,
input: openArray[uint8], inputIndex, len: int) {.inline.} =
memOrNot:
copyMem(addr(c.buffer[index]), unsafeAddr(input[inputIndex]), len)
do:
# cannot use system.`[]=` for arrays and openarrays as
# it can raise RangeDefect which gets tracked
for i in 0..<len:
c.buffer[index + i] = input[inputIndex + i]

proc md5Update*(c: var MD5Context, input: openArray[uint8]) =
## Updates the `MD5Context` with the `input` data.
##
## If you use the `toMD5 proc <#toMD5,string>`_, there's no need to call this
## function explicitly.
var input = input
var Index = int((c.count[0] shr 3) and 0x3F)
c.count[0] = c.count[0] + (uint32(len) shl 3)
if c.count[0] < (uint32(len) shl 3): c.count[1] = c.count[1] + 1'u32
c.count[1] = c.count[1] + (uint32(len) shr 29)
c.count[0] = c.count[0] + (uint32(input.len) shl 3)
if c.count[0] < (uint32(input.len) shl 3): c.count[1] = c.count[1] + 1'u32
c.count[1] = c.count[1] + (uint32(input.len) shr 29)
var PartLen = 64 - Index
if len >= PartLen:
copyMem(addr(c.buffer[Index]), input, PartLen)
transform(addr(c.buffer), c.state)
if input.len >= PartLen:
writeBuffer(c, Index, input, 0, PartLen)
transform(c.buffer, c.state)
var i = PartLen
while i + 63 < len:
transform(addr(input[i]), c.state)
while i + 63 < input.len:
transform(input.slice(i, i + 63), c.state)
inc(i, 64)
copyMem(addr(c.buffer[0]), addr(input[i]), len-i)
else:
copyMem(addr(c.buffer[Index]), addr(input[0]), len)
if i < input.len:
writeBuffer(c, 0, input, i, input.len - i)
elif input.len > 0:
writeBuffer(c, Index, input, 0, input.len)

proc md5Final*(c: var MD5Context, digest: var MD5Digest) =
## Finishes the `MD5Context` and stores the result in `digest`.
Expand All @@ -273,10 +350,10 @@ proc md5Final*(c: var MD5Context, digest: var MD5Digest) =
var Index = int((c.count[0] shr 3) and 0x3F)
if Index < 56: PadLen = 56 - Index
else: PadLen = 120 - Index
md5Update(c, padding, PadLen)
md5Update(c, cast[cstring](addr(Bits)), 8)
md5Update(c, padding.slice(0, PadLen - 1))
md5Update(c, Bits)
decode(digest, c.state)
zeroMem(addr(c), sizeof(MD5Context))
clearBuffer(c)


when defined(nimHasStyleChecks):
Expand Down
3 changes: 1 addition & 2 deletions tests/js/tstdlib_imports.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ import std/[
htmlgen,

# Hashing:
base64, hashes,
# fails due to cstring cast/zeroMem/copyMem/moveMem: md5
base64, hashes, md5,
# fails due to cstring cast/endians import: oids
# fails due to copyMem/endians import: sha1

Expand Down
19 changes: 14 additions & 5 deletions tests/stdlib/tmd5.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
discard """
targets: "c cpp js"
"""

import md5

doAssert(getMD5("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern") ==
"a3cca2b2aa1e3b5b3b5aad99a8529074")
doAssert(getMD5("Frank jagt im komplett verwahrlosten Taxi quer durch Bayern") ==
"7e716d0e702df0505fc72e2b89467910")
doAssert($toMD5("") == "d41d8cd98f00b204e9800998ecf8427e")
proc main() {.raises: [].} =
doAssert(getMD5("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern") ==
"a3cca2b2aa1e3b5b3b5aad99a8529074")
doAssert(getMD5("Frank jagt im komplett verwahrlosten Taxi quer durch Bayern") ==
"7e716d0e702df0505fc72e2b89467910")
doAssert($toMD5("") == "d41d8cd98f00b204e9800998ecf8427e")

main()

static: main()
3 changes: 1 addition & 2 deletions tests/test_nimscript.nims
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ import std/[
htmlgen,

# Hashing:
base64, hashes,
# fails due to cstring cast/zeroMem/copyMem/moveMem: md5
base64, hashes, md5,
# fails due to cstring cast/times import/endians import: oids
# fails due to copyMem/endians import: sha1

Expand Down