Addons updated to new doc format
[io.git] / addons / MD5 / source / IoMD5.c
blob43bd0cc63d53a511c4850acf1b8d5bb4b6870ae6
2 //metadoc MD5 copyright Steve Dekorte 2002
3 //metadoc MD5 license BSD revised
4 //metadoc MD5 category Digests
5 /*metadoc MD5 description
6 An object for calculating MD5 hashes. Each has calculation should instiate it's own MD5 instance.
7 */
9 #include "IoMD5.h"
10 #include "IoState.h"
11 #include "IoSeq.h"
13 #define DATA(self) ((IoMD5Data *)IoObject_dataPointer(self))
15 IoTag *IoMD5_newTag(void *state)
17 IoTag *tag = IoTag_newWithName_("MD5");
18 IoTag_state_(tag, state);
19 IoTag_freeFunc_(tag, (IoTagFreeFunc *)IoMD5_free);
20 IoTag_cloneFunc_(tag, (IoTagCloneFunc *)IoMD5_rawClone);
21 return tag;
24 IoMD5 *IoMD5_proto(void *state)
26 IoObject *self = IoObject_new(state);
27 IoObject_tag_(self, IoMD5_newTag(state));
29 IoObject_setDataPointer_(self, calloc(1, sizeof(IoMD5Data)));
30 io_md5_init(&(DATA(self)->mstate));
32 IoState_registerProtoWithFunc_(state, self, IoMD5_proto);
35 IoMethodTable methodTable[] = {
36 {"appendSeq", IoMD5_appendSeq},
37 {"md5", IoMD5_md5},
38 {"md5String", IoMD5_md5String},
39 {NULL, NULL},
41 IoObject_addMethodTable_(self, methodTable);
43 return self;
46 IoMD5 *IoMD5_rawClone(IoMD5 *proto)
48 IoObject *self = IoObject_rawClonePrimitive(proto);
49 IoObject_setDataPointer_(self, calloc(1, sizeof(IoMD5Data)));
50 io_md5_init(&(DATA(self)->mstate));
51 return self;
54 IoMD5 *IoMD5_new(void *state)
56 IoMD5 *proto = IoState_protoWithInitFunction_(state, IoMD5_proto);
57 return IOCLONE(proto);
60 void IoMD5_free(IoMD5 *self)
62 free(IoObject_dataPointer(self));
65 /* ----------------------------------------------------------- */
67 IoObject *IoMD5_appendSeq(IoMD5 *self, IoObject *locals, IoMessage *m)
69 /*doc MD5 appendSeq(aSequence)
70 Appends aSequence to the hash calculation. Returns self.
73 IoSeq *buffer = IoMessage_locals_seqArgAt_(m, locals, 0);
74 IOASSERT(DATA(self)->isDone == 0, "cannot append to an already calculated md5");
75 io_md5_append(&(DATA(self)->mstate),
76 (unsigned char const *)IoSeq_rawBytes(buffer),
77 IoSeq_rawSize(buffer));
78 return self;
81 UArray *IoMD5_md5UArray(IoMD5 *self)
83 if (DATA(self)->isDone == 0)
85 io_md5_finish(&(DATA(self)->mstate), DATA(self)->digest);
86 DATA(self)->isDone = 1;
89 return UArray_newWithData_type_size_copy_(DATA(self)->digest, CTYPE_uint8_t, 16, 1);
92 IoObject *IoMD5_md5(IoMD5 *self, IoObject *locals, IoMessage *m)
94 /*doc MD5 md5
95 Completes the MD5 calculation and returns the hash as a Buffer. Once this method is called, append() should not be called again on the receiver or it will raise an exception.
98 return IoSeq_newWithUArray_copy_(IOSTATE, IoMD5_md5UArray(self), 0);
101 IoObject *IoMD5_md5String(IoMD5 *self, IoObject *locals, IoMessage *m)
103 /*doc MD5 md5String
104 Returns a string containing a hexadecimal representation of the md5 hash.
107 UArray *ba = IoMD5_md5UArray(self);
108 UArray *baString = UArray_asNewHexStringUArray(ba);
109 UArray_free(ba);
110 return IoState_symbolWithUArray_copy_(IOSTATE, baString, 0);