Esempio n. 1
0
int main() {
  bignum l1 = from_str("99"),
    l2 = from_str("10");

  printf("%s\n", to_str(from_int(666)));
  printf("%d\n", to_int(from_str("666")));

  bignum sum = plus(l1, l2);
  printf("%s\n", to_str(sum));

  bignum diff = minus(l1, l2);
  printf("%s\n", to_str(diff));

  printf("%i\n", lesser(l1, l2));

  bignum muli = mul_int(l1, 2);
  printf("%s\n", to_str(muli));

  printf("%d\n", mod_int(l1, 10));

  bignum divi = div_int(l1, 10);
  printf("%s\n", to_str(divi));

  bignum mul = multiply(l1, l2);
  printf("%s\n", to_str(mul));

  bignum div = divide(l1, l2);
  printf("%s\n", to_str(div));

  bignum mod = modulo(l1, l2);
  printf("%s\n", to_str(mod));

  return 0;
}
Esempio n. 2
0
//创建公私钥
void rsa_create_key(vlong& e, vlong& d , vlong& m )
{
	unsigned char prand[2][128],tc;
	UINT i,j, nCount = 64;

	srand((unsigned)time(NULL));

	for(i=0; i<2; i++)
	{
		for(j=0; j<nCount; j++)
		{
			tc = (char)(0x41+rand()%0xAF);
			prand[i][j] = tc;
		}
		prand[i][j]=0;
	}

	// Choose primes
	prime_factory pf;

	vlong s1 = from_str((const char*)prand[0]);
	vlong s2 = from_str((const char*)prand[1]);

	vlong p = pf.find_prime( s1 );
	vlong q = pf.find_prime( s2 );

	// Calculate public key
	m = p*q;

	e = 65537;
	while ( gcd(p-1,e) != 1 || gcd(q-1,e) != 1 ) e += 2;

	// Calculate private key
	d = modinv( e, (p-1)*(q-1) );
}
Esempio n. 3
0
//创建公私钥
void rsa_create_key(RSA_KEY& pub, RSA_PRIVATE_KEY& pri)
{
	unsigned char prand[2][128],tc;
	UINT i,j, nCount = 64;

	srand((unsigned)time(NULL));

	for(i=0; i<2; i++)
	{
		for(j=0; j<nCount; j++)
		{
			tc = (char)(0x41+rand()%0xAF);
			prand[i][j] = tc;
		}
		prand[i][j]=0;
	}

	// Choose primes
	prime_factory pf;

	vlong s1 = from_str((const char*)prand[0]);
	vlong s2 = from_str((const char*)prand[1]);

	vlong tp = pf.find_prime( s1 );
	vlong tq = pf.find_prime( s2 );

	if ( tq > tp ) //make sure p > q
	{
		vlong tmp = tp;
		tp = tq;
		tq = tmp;
	}

	// Calculate public key
	pub.modulus = tp*tq;

	pub.key = 65537;
	while ( gcd(tp-1,pub.key) != 1 || gcd(tq-1,pub.key) != 1 ) pub.key += 2;

	// Calculate private key
	pri.p=tp;
	pri.q=tq;
	pri.dp=modinv(pub.key,tp-1);
	pri.dq=modinv(pub.key,tq-1);
	pri.qinv=modinv(tq,tp);
}
Esempio n. 4
0
int main(int argc, char const *argv[])
{
	FILE *f = fopen(argv[1], "r");
	if (!f) {
		printf("Could not open file %s\n", argv[1]);
		exit(EXIT_FAILURE);
	}

	/* read entire file into memory */
	char *contents = read_entire_file(f);
	fclose(f);

	/* solve all sudokus */
	struct timeval before;
	gettimeofday (&before, NULL);

	int result_size = 1024;
	char *result = malloc(sizeof(char) * result_size);
	strcpy(result, "");

	char *ptr = contents;
	int total_sudokus = strlen(contents) / (BOARD_SIZE*BOARD_SIZE);
	int sudokus_solved = 0;
	char solved[256];
	do {
		int sudoku[BOARD_SIZE][BOARD_SIZE];
		ptr = from_str(ptr, sudoku);
		solve(sudoku);
		to_str(sudoku, solved);
		result_size = append_str(&result, result_size, solved);
		load_bar(++sudokus_solved, total_sudokus, 100, 100);
	} while (*ptr);
	free(contents);

	struct timeval after;
	gettimeofday (&after, NULL);
	long duration = ((after.tv_sec * 1000000 + after.tv_usec) -
		(before.tv_sec * 1000000 + before.tv_usec)) / 1000;

	/* Write to file the solved sudokus */
	char output_name[64];
	sprintf(output_name, "solved_%s", argv[1]);
	FILE *output = fopen(output_name, "w");
	if (!output) {
		printf("Could not open file %s\n", output_name);
		exit(EXIT_FAILURE);
	}
	fprintf(output, "%s", result);
	free(result);
	fclose(output);

	/* Present time result */
	printf("-- Elapsed time: %d ms\n", duration);
	return 0;
}
Esempio n. 5
0
void private_key::create(const char * r1, const char * r2 )
{
	// Choose primes
	prime_factory pf;

	vlong s1 = from_str(r1);
	vlong s2 = from_str(r2);

	p = pf.find_prime( s1 );
	q = pf.find_prime( s2 );
	if ( p > q )  // *NOT* SAME AS PKCS#1 STANDARD
	{
		vlong tmp = p;
		p = q;
		q = tmp;
	}

	// Calculate public key
	m = p*q;
	e = 65537;
	while ( gcd(p-1,e) != 1 || gcd(q-1,e) != 1 )
		e += 2;
}
Esempio n. 6
0
ADTList list_from_str(char *str, void *(*from_str)(char *)) {
    ADTList list = list_new();
    int len = (int) strlen(str);
    for (int i = 0, escape = 0, start = 0; i <= len; i++) {
        if ((str[i] == ',' && !escape) || (i == len && len > 0)) {
            char *substr = str_substr(str, start, i - start);
            char *unescaped = str_unescape(substr);
            list_append(list, from_str(unescaped));
            start = i + 1;
            free(unescaped);
            free(substr);
        }
        if (str[i] == '\\' && !escape)
            escape = 1;
        else
            escape = 0;
    }
    return list;
}
// echo -180 -60 | cs2cs -f "%.10f" +init=epsg:4326 +to +init=epsg:3857
int main(int argc, char** argv)
{
    mapnik::parameters params;
    benchmark::handle_args(argc,argv,params);
    mapnik::box2d<double> from(-180,-80,180,80);
    mapnik::box2d<double> to(-20037508.3427892476,-15538711.0963092316,20037508.3427892476,15538711.0963092316);
    std::string from_str("+init=epsg:4326");
    std::string to_str("+init=epsg:3857");
    std::string from_str2("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
    std::string to_str2("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over");
    int return_value = 0;
    test test_runner(params,
                     from_str,
                     to_str,
                     from,
                     to,
                     true);
    return_value = return_value | run(test_runner,"lonlat->merc epsg");
    test test_runner2(params,
                     from_str2,
                     to_str2,
                     from,
                     to,
                     true);
    return_value = return_value | run(test_runner2,"lonlat->merc literal");
    test test_runner3(params,
                     to_str,
                     from_str,
                     to,
                     from,
                     true);
    return_value = return_value | run(test_runner3,"merc->lonlat epsg");
    test test_runner4(params,
                     to_str2,
                     from_str2,
                     to,
                     from,
                     true);
    return_value = return_value | run(test_runner4,"merc->lonlat literal");
    return return_value;
}
Esempio n. 8
0
 static bool convert(const std::string& i, T& o)
 {
     return from_str(i, o);
 }