Skip to content

Commit

Permalink
Added support for encrypting file
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajKataria authored and radare committed Mar 9, 2016
1 parent e78b293 commit 234b8d7
Showing 1 changed file with 104 additions and 57 deletions.
161 changes: 104 additions & 57 deletions binr/rahash2/rahash2.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2016 - pancake */
/* radare - LGPL - Copyright 2009-2016 - pancake */

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -457,7 +457,6 @@ int main(int argc, char **argv) {
} else {
eprintf ("Invalid key\n");
}

free (binseed);
return 0;
} else {
Expand Down Expand Up @@ -545,67 +544,115 @@ int main(int argc, char **argv) {

io = r_io_new ();
for (ret = 0, i = optind; i < argc; i++) {
switch (b64mode) {
case 1: // encode
{
int binlen;
char *out;
ut8 *bin = (ut8*)r_file_slurp (argv[i], &binlen);
if (!bin) {
eprintf ("Cannot open file\n");
continue;
}
out = malloc (((binlen + 1) * 4) / 3);
if (out) {
r_base64_encode (out, bin, binlen);
printf ("%s\n", out);
fflush (stdout);
free (out);
}
free (bin);
}
break;
case 2: // decode
{
int binlen, outlen;
ut8 *out, *bin = (ut8*)r_file_slurp (argv[i], &binlen);
if (!bin) {
eprintf ("Cannot open file\n");
continue;
}
out = malloc (binlen + 1);
if (out) {
outlen = r_base64_decode (out, (const char*)bin, binlen);
write (1, out, outlen);
free (out);
}
free (bin);
}
break;
default:
if (!strcmp (argv[i], "-")) {
int sz = 0;
ut8 *buf = (ut8*)r_stdin_slurp (&sz);
char *uri = r_str_newf ("malloc://%d", sz);
if (sz > 0) {
if (!r_io_open_nomap (io, uri, 0, 0)) {
eprintf ("rahash2: Cannot open malloc://1024\n");
return 1;
if (encrypt) {//for encrytion when files are provided
int seedlen = seed? strlen (seed): 0;
if (seedlen > 0) {
RCrypto *cry = r_crypto_new ();
if (r_crypto_use (cry, encrypt)) {
ut8 *binseed = malloc (seedlen + 1);
if (binseed) {
int len = r_hex_str2bin (seed, binseed);
if (len <1) {
len = seedlen;
strcpy ((char *)binseed, seed);
} else {
seedlen = len;
}
if (r_crypto_set_key (cry, binseed, seedlen, 0, 0)) {
int file_size;
ut8 *buf = (ut8*)r_file_slurp (argv[i], &file_size);
if (!buf) {
eprintf ("rahash2: Cannot open file\n");
continue;
}
r_crypto_update (cry, buf, file_size);
r_crypto_final (cry, NULL, 0);
int result_size = 0;
ut8 *result = r_crypto_get_output (cry, &result_size);
if (result) {
write (1, result, result_size);
free (result);
}
free(buf);
} else {
eprintf ("Invalid key\n");
}
free (binseed);
return 0;
} else {
eprintf ("Cannot allocate %d bytes\n", seedlen);
}
r_io_pwrite (io, 0, buf, sz);
} else {
eprintf ("Unknown encryption algorithm '%s'\n", encrypt);
}
free (uri);
r_crypto_free (cry);
} else {
if (r_file_is_directory (argv[i])) {
eprintf ("rahash2: Cannot hash directories\n");
return 1;
eprintf ("Encryption key not defined. Use -S [key]\n");
}
return 1;
} else {
switch (b64mode) {
case 1: // encode
{
int binlen;
char *out;
ut8 *bin = (ut8*)r_file_slurp (argv[i], &binlen);
if (!bin) {
eprintf ("Cannot open file\n");
continue;
}
out = malloc (((binlen + 1) * 4) / 3);
if (out) {
r_base64_encode (out, bin, binlen);
printf ("%s\n", out);
fflush (stdout);
free (out);
}
free (bin);
}
break;
case 2: // decode
{
int binlen, outlen;
ut8 *out, *bin = (ut8*)r_file_slurp (argv[i], &binlen);
if (!bin) {
eprintf ("Cannot open file\n");
continue;
}
if (!r_io_open_nomap (io, argv[i], 0, 0)) {
eprintf ("rahash2: Cannot open '%s'\n", argv[i]);
return 1;
out = malloc (binlen + 1);
if (out) {
outlen = r_base64_decode (out, (const char*)bin, binlen);
write (1, out, outlen);
free (out);
}
free (bin);
}
break;
default:
if (!strcmp (argv[i], "-")) {
int sz = 0;
ut8 *buf = (ut8*)r_stdin_slurp (&sz);
char *uri = r_str_newf ("malloc://%d", sz);
if (sz > 0) {
if (!r_io_open_nomap (io, uri, 0, 0)) {
eprintf ("rahash2: Cannot open malloc://1024\n");
return 1;
}
r_io_pwrite (io, 0, buf, sz);
}
free (uri);
} else {
if (r_file_is_directory (argv[i])) {
eprintf ("rahash2: Cannot hash directories\n");
return 1;
}
if (!r_io_open_nomap (io, argv[i], 0, 0)) {
eprintf ("rahash2: Cannot open '%s'\n", argv[i]);
return 1;
}
}
ret |= do_hash (argv[i], algo, io, bsize, rad, ule, compareBin);
}
ret |= do_hash (argv[i], algo, io, bsize, rad, ule, compareBin);
}
}
free (hashstr);
Expand Down

0 comments on commit 234b8d7

Please sign in to comment.