Hashing cmdline strings added.
[skeinsum.git] / skein_cli.c
index 4f13c7bd2e5d36279e43b4ae45e480b04960f734..e95f4b60844f9e90db443def874a87c9e453d678 100644 (file)
@@ -330,14 +330,16 @@ int VerifyHashesFromFile(FILE *fp, int status, int warn, int quiet)
                int hashVersion = decomposeHashLine(hash,MsgDigest_tmp,file_tmp);
                if (hashVersion == -1)
                {
-                       fprintf(stderr, "skein%d: %s is using newer version of skein%d algorithm\n",hashbitlen,file_tmp,hashbitlen);
-                       fprintf(stderr, "You should update your algorithm\n");
+                       WARN("%s is using newer version of skein%d algorithm\n"
+                            "You should update your algorithm\n",
+                            file_tmp, hashbitlen);
                        continue;
                }
                else if (hashVersion == 0)
                {
-                       fprintf(stderr, "skein%d: %s is using an older version of skein%d algorithm\n",hashbitlen,file_tmp,hashbitlen);
-                       fprintf(stderr, "You should use the older algorithm\n");
+                       WARN("%s is using an older version of skein%d algorithm\n"
+                            "You should use the older algorithm\n",
+                            file_tmp, hashbitlen);
                        continue;
                }
                else if (!isProper(MsgDigest_tmp))
@@ -378,27 +380,28 @@ void print_usage(void)
 {
        printf("Usage: skein%dsum [OPTION]... [FILE]...\n",hashbitlen);
        printf("Print or check skein (%d-bit) checksums.\n",hashbitlen);
-       printf("With no FILE, or when FILE is -, read standard input.\n");
-       printf("\n");
-       printf("-b, --binary         read in binary mode\n");
-       printf("-c, --check          read skein sums from the FILEs and check them\n");
-       printf("--tag            create a BSD-style checksum\n");
-       printf("-t, --text           read in text mode (default)\n");
-       printf("\n");
-       printf("The following three options are useful only when verifying checksums:\n");
-       printf("--quiet          don't print OK for each successfully verified file\n");
-       printf("--status         don't output anything, status code shows success\n");
-       printf("-w, --warn           warn about improperly formatted checksum lines\n");
-       printf("\n");
-       printf("--strict         with --check, exit non-zero for any invalid input\n");
-       printf("--help     display this help and exit\n");
-       printf("--version  output version information and exit\n");
-       printf("\n");
-       printf("The sums are computed as described in version 1.3 of the Skein\n");
-       printf("specification. When checking, the input should be a former output of\n");
-       printf("this program. The default mode is to print a line with checksum, a\n");
-       printf("character indicating input mode ('*' for binary, space for text), and\n");
-       printf("name for each FILE.\n");
+       printf("With no FILE, or when FILE is -, read standard input.\n"
+              "\n"
+              "-b, --binary     read in binary mode\n"
+              "-c, --check      read skein sums from the FILEs and check them\n"
+              "--tag            create a BSD-style checksum\n"
+              "-t, --text       read in text mode (default)\n"
+              "-0               hash strings from command line\n"
+              "\n"
+              "The following three options are useful only when verifying checksums:\n"
+              "--quiet          don't print OK for each successfully verified file\n"
+              "--status         don't output anything, status code shows success\n"
+              "-w, --warn       warn about improperly formatted checksum lines\n"
+              "\n"
+              "--strict         with --check, exit non-zero for any invalid input\n"
+              "-h, --help       display this help and exit\n"
+              "-V, --version    output version information and exit\n"
+              "\n"
+              "The sums are computed as described in version 1.3 of the Skein\n"
+              "specification. When checking, the input should be a former output of\n"
+              "this program. The default mode is to print a line with checksum, a\n"
+              "character indicating input mode ('*' for binary, space for text), and\n"
+              "name for each FILE.\n");
        exit(1);
 }
 
@@ -446,10 +449,10 @@ int main(int argc, char** argv)
 /*****************************************************************************************
  ************************************* GETTING DATA ***********************************
  *****************************************************************************************/
-       while ((opt = getopt_long (argc, argv, "hVbctw", long_options, NULL)) != -1)
+       while ((opt = getopt_long (argc, argv, "hVbctw0", long_options, NULL)) != -1)
        {
                switch (opt) {
-                       case 0              : hashString = 1;  break;
+                       case '0'            : hashString = 1;  break;
                        case 'b'            : binary     = 1;  break;
                        case 't'            : binary     = 0;  break;
                        case 'c'            : check      = 1;  break;
@@ -471,6 +474,23 @@ int main(int argc, char** argv)
  ************************************* PROCESSING DATA ***********************************
  *****************************************************************************************/
 
+       if (hashString > 0)
+       {
+               int n = first_file;
+               if (n >= argc) {
+                       WARN("command line should contain strings for hashing\n");
+                       TRYHELP_GOODBYE();
+               }
+               for( ; n < argc; n++) {
+                       unsigned char output[hashbitlen/4];
+                       char digest[hashbitlen/4 + 1];
+                       Hash(hashbitlen, argv[n], strlen(argv[n]), output);
+                       hash2hexstr(output, digest);
+                       printf("%s -%s\n", digest, argv[n]);
+               }
+               return 0;
+       }
+
        if (check < 0)   /* READ FILES, GENERATE CHECKSUMS AND PRINT TO STDOUT.. */
        {
                if (quiet == 1 || warn == 1 || status == 1)