Skip to content

Commit

Permalink
Add JSON parsing and remove trailing spaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhames committed Mar 4, 2012
1 parent 25e123b commit 773da50
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.scons
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ nacl_env = make_nacl_env.NaClEnvironment(
use_c_plus_plus_libs=True, nacl_platform=os.getenv('NACL_TARGET_PLATFORM'))

nacl_env.Append(
LIBS=['tomcrypt']
LIBS=['tomcrypt', 'jsoncpp']
)

sources = ['safr.cc']
Expand Down
58 changes: 37 additions & 21 deletions safr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,27 @@ void SafrInstance::HandleMessage(const pp::Var& var_message) {
PostMessage("FAIL");
}
} else {
std::string str = var_message.AsString();
const char* tmp = str.c_str();
PostMessage(tmp);
sha1sum(tmp);
md5sum(tmp);
Json::Value data;
Json::Reader reader;
bool parsed = reader.parse(var_message.AsString().c_str(), data);
if (!parsed) {
return;
}

std::string action = data.get("action", "").asString();
if (action == "md5") {
std::string str = data.get("text", "").asString();
if (str.empty()) return;

const char* tmp = str.c_str();
md5sum(tmp);
} else if (action == "sha1") {
std::string str = data.get("text", "").asString();
if (str.empty()) return;

const char* tmp = str.c_str();
sha1sum(tmp);
}
}
}

Expand All @@ -48,12 +64,12 @@ bool SafrInstance::register_algs() {
if (register_hash(&sha1_desc) == -1) return false;
if (register_hash(&sha256_desc) == -1) return false;
if (register_hash(&md5_desc) == -1) return false;

aes_idx = find_cipher("aes");
sha1_idx = find_hash("sha1");
sha256_idx = find_hash("sha256");
md5_idx = find_hash("md5");

return true;
}

Expand All @@ -66,11 +82,11 @@ bool SafrInstance::test() {
}

void SafrInstance::crypt (const char* salt, const char* text) {
if (aes_idx == -1) return;
if ((aes_idx == -1) || (sha256_idx == -1)) return;

ivsize = cipher_descriptor[aes_idx].block_length;
ks = hash_descriptor[sha256_idx].hashsize;
if (cipher_descriptor[aes_idx].keysize(&ks) != CRYPT_OK) {
if (cipher_descriptor[aes_idx].keysize(&ks) != CRYPT_OK) {
return;
}
}
Expand All @@ -80,33 +96,33 @@ void SafrInstance::decrypt (const char* salt, const char* str) {

void SafrInstance::sha1sum (const char* text) {
if (sha1_idx == -1) return;
unsigned char tmp[16];

unsigned char tmp[20];
hash_descriptor[sha1_idx].init(&md);
hash_descriptor[sha1_idx].process(&md, (unsigned char *)text,strlen(text));
hash_descriptor[sha1_idx].done(&md, tmp);

char buffer[32] = "";
char str[2];
for (x = 0; x < (int)hash_descriptor[sha1_idx].hashsize; x++) {
sprintf(str, "%02x",tmp[x]);
char str[4];
for (i = 0; i < (int)hash_descriptor[sha1_idx].hashsize; i++) {
sprintf(str, "%02x",tmp[i]);
strcat(buffer, str);
}
PostMessage(buffer);
}

void SafrInstance::md5sum (const char* text) {
if (md5_idx == -1) return;

unsigned char tmp[16];
hash_descriptor[md5_idx].init(&md);
hash_descriptor[md5_idx].process(&md, (unsigned char *)text,strlen(text));
hash_descriptor[md5_idx].done(&md, tmp);

char buffer[32] = "";
char str[2];
for (x = 0; x < (int)hash_descriptor[md5_idx].hashsize; x++) {
sprintf(str, "%02x",tmp[x]);
char str[4];
for (i = 0; i < (int)hash_descriptor[md5_idx].hashsize; i++) {
sprintf(str, "%02x",tmp[i]);
strcat(buffer, str);
}
PostMessage(buffer);
Expand Down
7 changes: 4 additions & 3 deletions safr.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cstdio>
#include <string>
#include <tomcrypt.h>
#include <json/json.h>
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
Expand All @@ -33,17 +34,17 @@ class SafrInstance : public pp::Instance {
explicit SafrInstance(PP_Instance instance) : pp::Instance(instance) {};
virtual ~SafrInstance() {};
void HandleMessage(const pp::Var& var_message);

private:
unsigned char plaintext[512],ciphertext[512];
unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
unsigned char inbuf[512]; /* i/o block size */
unsigned long outlen, ivsize, x, y, z;
symmetric_CTR ctr;
int sha1_idx, sha256_idx, md5_idx, aes_idx, ks;
int sha1_idx, sha256_idx, md5_idx, aes_idx, ks, i, j, k;
unsigned long w;
hash_state md;

bool register_algs();
bool test();
void crypt (const char* salt, const char* text);
Expand Down

0 comments on commit 773da50

Please sign in to comment.