summary refs log tree commit diff stats
path: root/lib/wrappers/openssl.nim
diff options
context:
space:
mode:
authorBillingsly Wetherfordshire <phowl.mouth@gmail.com>2014-04-20 23:38:45 -0500
committerBillingsly Wetherfordshire <phowl.mouth@gmail.com>2014-04-20 23:38:45 -0500
commita7db33e87640d4f3dc57cfc261763b690de3fd58 (patch)
treef301b9d0a6f72447870a27d326c47c48d3dfed27 /lib/wrappers/openssl.nim
parent41f82fb45f3db4461fd7b5d65b4d6dd55c3c4a68 (diff)
downloadNim-a7db33e87640d4f3dc57cfc261763b690de3fd58.tar.gz
added md5 functions from <openssl/md5.h>
Diffstat (limited to 'lib/wrappers/openssl.nim')
-rw-r--r--lib/wrappers/openssl.nim67
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/wrappers/openssl.nim b/lib/wrappers/openssl.nim
index 90c398dce..41ee52522 100644
--- a/lib/wrappers/openssl.nim
+++ b/lib/wrappers/openssl.nim
@@ -433,3 +433,70 @@ else:
   proc SSLGetMode(s: PSSL): int = 
     result = SSLctrl(s, SSL_CTRL_MODE, 0, nil)
 
+# <openssl/md5.h>
+type 
+  MD5_LONG* = cuint
+const 
+  MD5_CBLOCK* = 64
+  MD5_LBLOCK* = int(MD5_CBLOCK div 4)
+  MD5_DIGEST_LENGTH* = 16
+type 
+  MD5_CTX* {.importc.} = object 
+    A,B,C,D,Nl,Nh: MD5_LONG
+    data: array[MD5_LBLOCK, MD5_LONG]
+    num: cuint
+
+{.pragma: ic, importc: "$1".}
+{.push callconv:cdecl, dynlib:DLLUtilName.}
+proc MD5_Init*(c: var MD5_CTX): cint{.ic.}
+proc MD5_Update*(c: var MD5_CTX; data: pointer; len: csize): cint{.ic.}
+proc MD5_Final*(md: cstring; c: var MD5_CTX): cint{.ic.}
+proc MD5*(d: ptr cuchar; n: csize; md: ptr cuchar): ptr cuchar{.ic.}
+proc MD5_Transform*(c: var MD5_CTX; b: ptr cuchar){.ic.}
+{.pop.}
+
+from strutils import toHex,toLower
+
+proc hexStr (buf:cstring): string =
+  # turn md5s output into a nice hex str 
+  result = newStringOfCap(32)
+  for i in 0 .. <16:
+    result.add toHex(buf[i].ord, 2).toLower
+
+proc MD5_File* (file: string): string {.raises:[EIO,Ebase].} =
+  ## Generate MD5 hash for a file. Result is a 32 character
+  # hex string with lowercase characters (like the output
+  # of `md5sum`
+  const
+    sz = 512
+  let f = open(file,fmRead)
+  var
+    buf: array[sz,char]
+    ctx: MD5_CTX
+
+  discard md5_init(ctx)
+  while(let bytes = f.readChars(buf, 0, sz); bytes > 0):
+    discard md5_update(ctx, buf[0].addr, bytes)
+
+  discard md5_final( buf[0].addr, ctx )
+  f.close
+  
+  result = hexStr(buf)
+
+proc MD5_Str* (str:string): string {.raises:[EIO].} =
+  ##Generate MD5 hash for a string. Result is a 32 character
+  #hex string with lowercase characters
+  var 
+    ctx: MD5_CTX
+    res: array[MD5_DIGEST_LENGTH,char]
+    input = str.cstring
+  discard md5_init(ctx)
+
+  var i = 0
+  while i < str.len:
+    let L = min(str.len - i, 512)
+    discard md5_update(ctx, input[i].addr, L)
+    i += L
+
+  discard md5_final(res,ctx)
+  result = hexStr(res)