Beispiel #1
0
int
main(int argc, char **argv)
{
    GError *error = NULL;
    int retval = EXIT_SUCCESS;
    EVP_PKEY *pubkey;

    if (!process_arguments(&argc, &argv, &error)) {
        retval = handle_error(error);
        g_clear_error(&error);
    } else if ((pubkey = read_public_key(option_key_file, &error)) == NULL) {
        retval = handle_error(error);
        g_clear_error(&error);
    } else {
        FILE *infile = fopen(option_data_file, "rb");

        if (infile == NULL) {
            fprintf(stderr, "%s: %s\n", option_data_file, strerror(errno));
            retval = EXIT_IO_ERROR;
        } else {
            uint8_t hash[SHA256_DIGEST_LENGTH];
            uint8_t signature[SIGNATURE_SIZE];

            if (fread(signature, 1, sizeof(signature), infile) !=
                sizeof(signature)) {
                if (ferror(infile)) {
                    fprintf(
                        stderr, "%s: %s\n", option_data_file, strerror(errno));
                    retval = EXIT_IO_ERROR;
                } else {
                    fprintf(stderr,
                            "%s: File too short for signature\n",
                            option_data_file);
                    retval = EXIT_FORMAT_ERROR;
                }
            } else if (!hash_stream(infile, hash, &error) ||
                       !verify_signature(hash, signature, pubkey, &error)) {
                retval = handle_error(error);
                g_clear_error(&error);
            } else if (option_out_file &&
                       !write_out_file(infile, option_out_file, &error)) {
                retval = handle_error(error);
                g_clear_error(&error);
            }

            fclose(infile);
        }

        EVP_PKEY_free(pubkey);
    }

    return retval;
}
Beispiel #2
0
int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
{
    int fd;
    if ((fd = open(filename, mode)) < 0) {
        perror(filename);
        return 0;
    }
    if (!hash_stream(ctx, fd, sum)) {
        perror(filename);
        return 0;
    }
    close(fd);
    return 1;
}
void FFM::avito_init_params(const std::string & data_conf_path)
{
    bool debug = false;

    std::ifstream config_stream((char*)data_conf_path.c_str(), std::ios::in);

    std::string line;
    std::string token;
    std::string header;

    // Read header:
    for(unsigned int i = 0; i < 8; i++){
      getline(config_stream, line);
    }

    std::istringstream line_stream(line);
    while(line_stream){	if (!getline( line_stream, token, ':' )) break;	}
    if(debug) std::cout << "Default hash: " << token << std::endl;

    bool useDefaultHash = ( token != "false" ) ? true : false;
    if ( useDefaultHash ){
      num_features = atoi(token.c_str());
    }

    getline(config_stream, line);
    getline(config_stream, line);

    unsigned int feature_count = 0;
    while(getline(config_stream, line)){
      std::istringstream hash_stream(line);
      getline( hash_stream, header, ':' );
      getline( hash_stream, token, ':' );
      
      num_fields++;
      if ( !useDefaultHash ) {
	num_features += atoi(token.c_str());
      }
      if(debug) std::cout << header << ": " << token << " Current inputDim: " << num_features << std::endl;
      feature_count++;
    }

    if(debug) std::cout << "Num-features: " << num_features << std::endl;
    
    initParams();    
}
Beispiel #4
0
int main (int argc, char **argv)
{
    int c, i;

    int verbose = 0;

    int errors = 0;

    int open_mode = O_RDONLY;

    gost_subst_block *b = &GostR3411_94_CryptoProParamSet;

    FILE *check_file = NULL;

    gost_hash_ctx ctx;

    while ((c = getopt (argc, argv, "bc::tv")) != -1)
    {
        switch (c)
        {
            case 'v':
                verbose = 1;
                break;
            case 't':
                b = &GostR3411_94_TestParamSet;
                break;
            case 'b':
                open_mode |= O_BINARY;
                break;
            case 'c':
                if (optarg)
                {
                    check_file = fopen (optarg, "r");
                    if (!check_file)
                    {
                        perror (optarg);
                        exit (2);
                    }
                }
                else
                {
                    check_file = stdin;
                }
                break;
            default:
                fprintf (stderr, "invalid option %c", optopt);
                help ();
        }
    }
    init_gost_hash_ctx (&ctx, b);
    if (check_file)
    {
        char inhash[65], calcsum[65], filename[PATH_MAX];

        int failcount = 0, count = 0;;
        if (check_file == stdin && optind < argc)
        {
            check_file = fopen (argv[optind], "r");
            if (!check_file)
            {
                perror (argv[optind]);
                exit (2);
            }
        }
        while (get_line (check_file, inhash, filename))
        {
            if (!hash_file (&ctx, filename, calcsum, open_mode))
            {
                exit (2);
            }
            count++;
            if (!strncmp (calcsum, inhash, 65))
            {
                if (verbose)
                {
                    fprintf (stderr, "%s\tOK\n", filename);
                }
            }
            else
            {
                if (verbose)
                {
                    fprintf (stderr, "%s\tFAILED\n", filename);
                }
                else
                {
                    fprintf (stderr, "%s: GOST hash sum check failed for '%s'\n", argv[0], filename);
                }
                failcount++;
            }
        }
        if (verbose && failcount)
        {
            fprintf (stderr, "%s: %d of %d file(f) failed GOST hash sum check\n", argv[0], failcount, count);
        }
        exit (failcount ? 1 : 0);
    }
    if (optind == argc)
    {
        char sum[65];

        if (!hash_stream (&ctx, fileno (stdin), sum))
        {
            perror ("stdin");
            exit (1);
        }
        printf ("%s -\n", sum);
        exit (0);
    }
    for (i = optind; i < argc; i++)
    {
        char sum[65];

        if (!hash_file (&ctx, argv[i], sum, open_mode))
        {
            errors++;
        }
        else
        {
            printf ("%s %s\n", sum, argv[i]);
        }
    }
    exit (errors ? 1 : 0);
}
Beispiel #5
0
void read_stdin(entry** table) {
	hash_stream(stdin, table);
}
Beispiel #6
0
void read_file(char *path, entry** table) {
	FILE *file = fopen(path, "r");
	assert(file);
	hash_stream(file, table);
	fclose(file);
}
Beispiel #7
0
int main(int argc, char **argv)
{
    int c, i;
    int verbose = 0;
    int errors = 0;
    int open_mode = O_RDONLY;
    FILE *check_file = NULL;
    int filenames_from_stdin = 0;
    gost_hash_ctx ctx;

    while ((c = getopt(argc, argv, "bxlvc::")) != -1) {
        switch (c) {
        case 'b':
            open_mode = open_mode | O_BINARY;
            break;
        case 'v':
            verbose = 1;
            break;
        case 'l':
            hashsize = 512;
            break;
        case 'x':
            filenames_from_stdin = 1;
            break;
        case 'c':
            if (optarg) {
                check_file = fopen(optarg, "r");
                if (!check_file) {
                    perror(optarg);
                    exit(2);
                }
            } else {
                check_file = stdin;
            }
            break;
        default:
            fprintf(stderr, "invalid option %c", optopt);
            help();
        }
    }
    if (check_file) {
        char inhash[MAX_HASH_SIZE + 1], calcsum[MAX_HASH_SIZE + 1],
            filename[PATH_MAX];
        int failcount = 0, count = 0;;
        if (check_file == stdin && optind < argc) {
            check_file = fopen(argv[optind], "r");
            if (!check_file) {
                perror(argv[optind]);
                exit(2);
            }
        }
        while (get_line(check_file, inhash, filename, verbose)) {
            count++;
            if (!hash_file(&ctx, filename, calcsum, open_mode)) {
                errors++;
                continue;
            }
            if (!strncmp(calcsum, inhash, hashsize / 4 + 1)) {
                if (verbose) {
                    fprintf(stderr, "%s\tOK\n", filename);
                }
            } else {
                if (verbose) {
                    fprintf(stderr, "%s\tFAILED\n", filename);
                } else {
                    fprintf(stderr,
                            "%s: GOST hash sum check failed for '%s'\n",
                            argv[0], filename);
                }
                failcount++;
            }
        }
        if (errors) {
            fprintf(stderr,
                    "%s: WARNING %d of %d file(s) cannot be processed\n",
                    argv[0], errors, count);

        }
        if (failcount) {
            fprintf(stderr,
                    "%s: WARNING %d of %d file(s) failed GOST hash sum check\n",
                    argv[0], failcount, count - errors);
        }
        exit((failcount || errors) ? 1 : 0);
    } else if (filenames_from_stdin) {
        char sum[65];
        char filename[PATH_MAX + 1], *end;
        while (!feof(stdin)) {
            if (!fgets(filename, PATH_MAX, stdin))
                break;
            for (end = filename; *end; end++) ;
            end--;
            for (; *end == '\n' || *end == '\r'; end--)
                *end = 0;
            if (!hash_file(&ctx, filename, sum, open_mode)) {
                errors++;
            } else {
                printf("%s %s\n", sum, filename);
            }
        }
    } else if (optind == argc) {
        char sum[65];
#ifdef _WIN32
        if (open_mode & O_BINARY) {
            _setmode(fileno(stdin), O_BINARY);
        }
#endif
        if (!hash_stream(&ctx, fileno(stdin), sum)) {
            perror("stdin");
            exit(1);
        }
        printf("%s -\n", sum);
        exit(0);
    } else {
        for (i = optind; i < argc; i++) {
            char sum[65];
            if (!hash_file(&ctx, argv[i], sum, open_mode)) {
                errors++;
            } else {
                printf("%s %s\n", sum, argv[i]);
            }
        }
    }
    exit(errors ? 1 : 0);
}
Beispiel #8
0
int main(int argc,char **argv)
	{
	int c,i;
	int verbose=0;
	int errors=0;
	int open_mode = O_RDONLY;
	gost_subst_block *b=  &GostR3411_94_CryptoProParamSet;
	TINYCLR_SSL_FILE *check_file = NULL;
	gost_hash_ctx ctx;
	
	while( (c=getopt(argc,argv,"bc::tv"))!=-1)
		{
		switch (c)
			{
			case 'v': verbose=1; break;
			case 't': b= &GostR3411_94_TestParamSet; break;
			case 'b': open_mode |= O_BINARY; break;
			case 'c':
				if (optarg)
					{
					check_file = TINYCLR_SSL_FOPEN(optarg,"r");
					if (!check_file)
						{
						TINYCLR_SSL_PERROR(optarg);
						TINYCLR_SSL_EXIT(2);
						}
					}
				else
					{
				  	check_file= OPENSSL_TYPE__FILE_STDIN;
					}
				break;
			default:
				TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR,"invalid option %c",optopt);
				help();
			}
		}
	init_gost_hash_ctx(&ctx,b);
	if (check_file)
		{
		char inhash[65],calcsum[65],filename[PATH_MAX];
		int failcount=0,count=0;;
		if (check_file==OPENSSL_TYPE__FILE_STDIN && optind<argc)
			{
			check_file=TINYCLR_SSL_FOPEN(argv[optind],"r");
			if (!check_file)
				{	
				TINYCLR_SSL_PERROR(argv[optind]);
				TINYCLR_SSL_EXIT(2);
				}
			}	
		while (get_line(check_file,inhash,filename))
			{
			if (!hash_file(&ctx,filename,calcsum,open_mode))
				{
				TINYCLR_SSL_EXIT (2);
				}	
			count++;
			if (!TINYCLR_SSL_STRNCMP(calcsum,inhash,65))
				{
				if (verbose)
					{
					TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR,"%s\tOK\n",filename);
					}
				}
			else
				{
				if (verbose)
					{
					TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR,"%s\tFAILED\n",filename);
					}
				else
					{
					TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR,"%s: GOST hash sum check failed for '%s'\n",
						argv[0],filename);
					}
				failcount++;
				}
			}	
		if (verbose && failcount)
			{
			TINYCLR_SSL_FPRINTF(OPENSSL_TYPE__FILE_STDERR,"%s: %d of %d file(f) failed GOST hash sum check\n",
				argv[0],failcount,count);
			}
		TINYCLR_SSL_EXIT (failcount?1:0);
		}
	if (optind==argc)
		{
		char sum[65];
		if (!hash_stream(&ctx,TINYCLR_SSL_FILENO(OPENSSL_TYPE__FILE_STDIN),sum))
			{
			TINYCLR_SSL_PERROR("OPENSSL_TYPE__FILE_STDIN");
			TINYCLR_SSL_EXIT(1);
			}	
		TINYCLR_SSL_PRINTF("%s -\n",sum);
		TINYCLR_SSL_EXIT(0);
		}	
	for (i=optind;i<argc;i++)
		{
		char sum[65];
		if (!hash_file(&ctx,argv[i],sum,open_mode))
			{
			errors++;
			}
		else
			{	
			TINYCLR_SSL_PRINTF("%s %s\n",sum,argv[i]);
			}
		}	
	TINYCLR_SSL_EXIT(errors?1:0);	
	}