sha256c.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package require critcl;
00011 package provide sha256c 1.0.2
00012
00013 critcl::cheaders sha256.h;
00014 critcl::csources sha256.c;
00015
00016 if {$tcl_platform(byteOrder) eq "littleEndian"} {
00017 byteOrder = 1234
00018 } else {
00019 byteOrder = 4321
00020 }
00021 critcl::cheaders -DTCL_BYTE_ORDER=$byteOrder
00022
00023 namespace ::sha2 {
00024
00025 catch {
00026 critcl::debug memory symbols
00027 }
00028
00029 critcl::ccode {
00030
00031
00032
00033
00034 static
00035 Tcl_ObjType sha256_type;
00036
00037 static void
00038 sha256_free_rep(Tcl_Obj* obj)
00039 {
00040 SHA256_CTX* mp = (SHA256_CTX*) obj->internalRep.otherValuePtr;
00041 free(mp);
00042 }
00043
00044 static void
00045 sha256_dup_rep(Tcl_Obj* obj, Tcl_Obj* dup)
00046 {
00047 SHA256_CTX* mp = (SHA256_CTX*) obj->internalRep.otherValuePtr;
00048 dup->internalRep.otherValuePtr = malloc(sizeof *mp);
00049 memcpy(dup->internalRep.otherValuePtr, mp, sizeof *mp);
00050 dup->typePtr = &sha256_type;
00051 }
00052
00053 static void
00054 sha256_string_rep(Tcl_Obj* obj)
00055 {
00056 unsigned char buf[SHA256_HASH_SIZE];
00057 Tcl_Obj* temp;
00058 char* str;
00059 SHA256_CTX dup = *(SHA256_CTX*) obj->internalRep.otherValuePtr;
00060
00061 SHA256Final(&dup, buf);
00062
00063
00064 temp = Tcl_NewByteArrayObj(buf, sizeof buf);
00065 Tcl_IncrRefCount(temp);
00066
00067 str = Tcl_GetStringFromObj(temp, &obj->length);
00068 obj->bytes = Tcl_Alloc(obj->length + 1);
00069 memcpy(obj->bytes, str, obj->length + 1);
00070
00071 Tcl_DecrRefCount(temp);
00072 }
00073
00074 static int
00075 sha256_from_any(Tcl_Interp* ip, Tcl_Obj* obj)
00076 {
00077 assert(0);
00078 return TCL_ERROR;
00079 }
00080
00081 static
00082 Tcl_ObjType sha256_type = {
00083 "sha256c", sha256_free_rep, sha256_dup_rep, sha256_string_rep,
00084 sha256_from_any
00085 };
00086 }
00087
00088 critcl::ccommand sha256c_init256 {dummy ip objc objv} {
00089 SHA256_CTX* mp;
00090 unsigned char* data;
00091 int size;
00092 Tcl_Obj* obj;
00093
00094 if (objc > 1) {
00095 Tcl_WrongNumArgs(ip, 1, objv, "");
00096 return TCL_ERROR;
00097 }
00098
00099 obj = Tcl_NewObj();
00100 mp = (SHA256_CTX*) malloc(sizeof *mp);
00101 SHA256Init(mp);
00102
00103 if (obj->typePtr != NULL && obj->typePtr->freeIntRepProc != NULL) {
00104 obj->typePtr->freeIntRepProc(obj);
00105 }
00106
00107 obj->internalRep.otherValuePtr = mp;
00108 obj->typePtr = &sha256_type;
00109
00110 Tcl_SetObjResult(ip, obj);
00111 Tcl_IncrRefCount(obj);
00112 Tcl_InvalidateStringRep(obj);
00113 return TCL_OK;
00114 }
00115
00116 critcl::ccommand sha256c_init224 {dummy ip objc objv} {
00117 SHA256_CTX* mp;
00118 unsigned char* data;
00119 int size;
00120 Tcl_Obj* obj;
00121
00122 if (objc > 1) {
00123 Tcl_WrongNumArgs(ip, 1, objv, "");
00124 return TCL_ERROR;
00125 }
00126
00127 obj = Tcl_NewObj();
00128 mp = (SHA256_CTX*) malloc(sizeof *mp);
00129 SHA224Init(mp);
00130
00131 if (obj->typePtr != NULL && obj->typePtr->freeIntRepProc != NULL) {
00132 obj->typePtr->freeIntRepProc(obj);
00133 }
00134
00135 obj->internalRep.otherValuePtr = mp;
00136 obj->typePtr = &sha256_type;
00137
00138 Tcl_SetObjResult(ip, obj);
00139 Tcl_IncrRefCount(obj);
00140 Tcl_InvalidateStringRep(obj);
00141 return TCL_OK;
00142 }
00143
00144 critcl::ccommand sha256c_update {dummy ip objc objv} {
00145 SHA256_CTX* mp;
00146 unsigned char* data;
00147 int size;
00148 Tcl_Obj* obj;
00149
00150 if (objc != 3) {
00151 Tcl_WrongNumArgs(ip, 1, objv, "data context");
00152 return TCL_ERROR;
00153 }
00154
00155 if (objv[2]->typePtr != &sha256_type
00156 && sha256_from_any(ip, objv[2]) != TCL_OK) {
00157 return TCL_ERROR;
00158 }
00159
00160 obj = objv[2];
00161 if (Tcl_IsShared(obj)) {
00162 obj = Tcl_DuplicateObj(obj);
00163 }
00164
00165 Tcl_SetObjResult(ip, obj);
00166 Tcl_IncrRefCount(obj);
00167
00168 Tcl_InvalidateStringRep(obj);
00169 mp = (SHA256_CTX*) obj->internalRep.otherValuePtr;
00170
00171 data = Tcl_GetByteArrayFromObj(objv[1], &size);
00172 SHA256Update(mp, data, size);
00173
00174 return TCL_OK;
00175 }
00176 }
00177