1
# Wrapper for RSA's Message Digest in C
3
package provide md5c 0.11
6
critcl::cheaders md5c_c/md5.h
7
critcl::csources md5c_c/md5.c
16
Tcl_ObjType md5_type; /* fast internal access representation */
19
md5_free_rep(Tcl_Obj* obj)
21
MD5_CTX* mp = (MD5_CTX*) obj->internalRep.otherValuePtr;
26
md5_dup_rep(Tcl_Obj* obj, Tcl_Obj* dup)
28
MD5_CTX* mp = (MD5_CTX*) obj->internalRep.otherValuePtr;
29
dup->internalRep.otherValuePtr = malloc(sizeof *mp);
30
memcpy(dup->internalRep.otherValuePtr, mp, sizeof *mp);
31
dup->typePtr = &md5_type;
35
md5_string_rep(Tcl_Obj* obj)
37
unsigned char buf[16];
40
MD5_CTX dup = *(MD5_CTX*) obj->internalRep.otherValuePtr;
44
/* convert via a byte array to properly handle null bytes */
45
temp = Tcl_NewByteArrayObj(buf, sizeof buf);
46
Tcl_IncrRefCount(temp);
48
str = Tcl_GetStringFromObj(temp, &obj->length);
49
obj->bytes = Tcl_Alloc(obj->length + 1);
50
memcpy(obj->bytes, str, obj->length + 1);
52
Tcl_DecrRefCount(temp);
56
md5_from_any(Tcl_Interp* ip, Tcl_Obj* obj)
63
Tcl_ObjType md5_type = {
64
"md5c", md5_free_rep, md5_dup_rep, md5_string_rep, md5_from_any
68
critcl::ccommand md5c {dummy ip objc objv} {
74
//Tcl_RegisterObjType(&md5_type);
76
if (objc < 2 || objc > 3) {
77
Tcl_WrongNumArgs(ip, 1, objv, "data ?context?");
82
if (objv[2]->typePtr != &md5_type && md5_from_any(ip, objv[2]) != TCL_OK)
85
if (Tcl_IsShared(obj))
86
obj = Tcl_DuplicateObj(obj);
89
mp = (MD5_CTX*) malloc(sizeof *mp);
92
if (obj->typePtr != NULL && obj->typePtr->freeIntRepProc != NULL)
93
obj->typePtr->freeIntRepProc(obj);
95
obj->internalRep.otherValuePtr = mp;
96
obj->typePtr = &md5_type;
99
Tcl_SetObjResult(ip, obj);
100
Tcl_IncrRefCount(obj); //!! huh?
102
Tcl_InvalidateStringRep(obj);
103
mp = (MD5_CTX*) obj->internalRep.otherValuePtr;
105
data = Tcl_GetByteArrayFromObj(objv[1], &size);
106
MD5Update(mp, data, size);
111
if {[info exists pkgtest] && $pkgtest} {
114
foreach {msg expected} {
116
"d41d8cd98f00b204e9800998ecf8427e"
118
"0cc175b9c0f1b6a831c399e269772661"
120
"900150983cd24fb0d6963f7d28e17f72"
122
"f96b697d7cb7938d525a2f31aaf161d0"
123
"abcdefghijklmnopqrstuvwxyz"
124
"c3fcd3d76192e4007dfb496cca67e13b"
125
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
126
"d174ab98d277d9f5a5611c2c9f419d9f"
127
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
128
"57edf4a22be3c955ac49da2e2107b67a"
130
puts "testing: md5 \"$msg\""
131
binary scan [md5c $msg] H* computed
132
puts "computed: $computed"
133
if {0 != [string compare $computed $expected]} {
134
puts "expected: $expected"
139
foreach len {10 50 100 500 1000 5000 10000} {
140
set blanks [format %$len.0s ""]
141
puts "input length $len: [time {md5c $blanks} 1000]"