added -C to generate a blake2 checksum of one or more files

This commit is contained in:
git@daemon.de
2015-07-17 16:22:12 +02:00
parent 6dfb15ade6
commit 0811957a46
12 changed files with 114 additions and 14 deletions

View File

@@ -359,3 +359,30 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
return 1;
}
void pcpchecksum(char **files, int filenum) {
int i;
byte *checksum = ucmalloc(crypto_generichash_BYTES_MAX);
for(i=0; i<filenum; i++) {
FILE *in;
if(files[i] == NULL)
in = stdin;
else {
if((in = fopen(files[i], "rb")) == NULL) {
fatal(ptx, "Could not open input file %s\n", files[i]);
break;
}
}
Pcpstream *pin = ps_new_file(in);
if(pcp_checksum(ptx, pin, checksum) > 0) {
char *hex = _bin2hex(checksum, crypto_generichash_BYTES_MAX);
fprintf(stdout, "BLAKE2 (%s) = %s\n", files[i], hex);
free(hex);
}
else
break;
}
free(checksum);
}

View File

@@ -40,5 +40,6 @@
int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, int verify);
int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *recipient, int signcrypt, int armor, int anon);
void pcpchecksum(char **files, int filenum);
#endif /* _HAVE_ENCRYPTION_H */

View File

@@ -114,6 +114,7 @@ int main (int argc, char **argv) {
{ "decrypt", no_argument, NULL, 'd' },
{ "anonymous", no_argument, NULL, 'A' },
{ "add-myself", no_argument, NULL, 'M' },
{ "checksum", no_argument, NULL, 'C' },
/* encoding */
{ "z85-encode", no_argument, NULL, 'z' },
@@ -135,7 +136,7 @@ int main (int argc, char **argv) {
{ NULL, 0, NULL, 0 }
};
while ((opt = getopt_long(argc, argv, "klLV:vdehsO:i:I:pSPRtEx:DzaZr:gcmf:b1F:0KAMX:j",
while ((opt = getopt_long(argc, argv, "klLV:vdehsO:i:I:pSPRtEx:DzaZr:gcmf:b1F:0KAMX:jC",
longopts, NULL)) != -1) {
switch (opt) {
@@ -230,6 +231,9 @@ int main (int argc, char **argv) {
mode += PCP_MODE_VERIFY;
usevault = 1;
break;
case 'C':
mode += PCP_MODE_CHECKSUM;
break;
case 'f':
sigfile = ucmalloc(strlen(optarg)+1);
strncpy(sigfile, optarg, strlen(optarg)+1);
@@ -433,6 +437,14 @@ int main (int argc, char **argv) {
if(usevault == 1) {
vault = pcpvault_init(ptx, vaultfile);
/* special case: ignore vault error in decrypt mode. sym decrypt doesn't
need it and asym will just fail without keys. */
if(vault == NULL && mode == PCP_MODE_DECRYPT) {
/* use an empty one */
vault = pcpvault_init(ptx, "/dev/null");
fatals_reset(ptx);
}
if(vault != NULL) {
switch (mode) {
case PCP_MODE_KEYGEN:
@@ -595,7 +607,24 @@ int main (int argc, char **argv) {
pcpvault_close(ptx, vault);
}
break;
case PCP_MODE_CHECKSUM:
if(infile == NULL) {
if(argc == 0) {
char *list[1];
list[0] = NULL;
pcpchecksum(list, 1);
}
else {
pcpchecksum(argv, argc);
}
}
else {
char *list[1];
list[0] = infile;
pcpchecksum(list, 1);
}
break;
default:
/* mode params mixed */
fatal(ptx, "Sorry, invalid combination of commandline parameters (0x%04X)!\n", mode);

View File

@@ -70,7 +70,7 @@
#define PCP_MODE_ZDECODE 0x00000962
#define PCP_MODE_SIGN 0x00000FF6
#define PCP_MODE_VERIFY 0x00001B25
#define PCP_MODE_YAML 0x00002E27
#define PCP_MODE_CHECKSUM 0x00002E27
/*
0x00001B25

View File

@@ -45,6 +45,11 @@ Signature Options:
Encoding Options:
-z --z85-encode Armor with Z85 encoding.
-Z --z85-decode Decode Z85 encodeded input.
-a --armor --textmode same as -z
Misc Options:
-C --checksum calculate a Blake2 checksum of one or more files.
Arguments:
Extra arguments after options are treated as filenames or