diff options
author | Maxim Biro <nurupo.contributions@gmail.com> | 2014-06-20 22:21:58 -0400 |
---|---|---|
committer | Maxim Biro <nurupo.contributions@gmail.com> | 2014-06-20 22:28:22 -0400 |
commit | b9010540b6a85198c90cf7850771c2f41cb23f24 (patch) | |
tree | baa0058fb97478773fc4242846cfc09fee1974e2 /other/fun | |
parent | 760333b9076cb5e9e89f5a296cf5afa92bec7e66 (diff) |
Added strkey
Diffstat (limited to 'other/fun')
-rw-r--r-- | other/fun/strkey.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/other/fun/strkey.c b/other/fun/strkey.c new file mode 100644 index 00000000..fd1caa87 --- /dev/null +++ b/other/fun/strkey.c | |||
@@ -0,0 +1,130 @@ | |||
1 | /* strkey -- String in Public Key | ||
2 | * | ||
3 | * Generates Tox's key pairs, checking if a certain string is in the public key. | ||
4 | * | ||
5 | * Requires sodium or nacl library. | ||
6 | * | ||
7 | * There seem to be some problems with the code working on Windows -- it works | ||
8 | * when built in debug mode with MinGW 4.8, but it doesn't work correctly when | ||
9 | * built in release. | ||
10 | * | ||
11 | * Usage: strkey <offset> <string> | ||
12 | * | ||
13 | * Offset - an integer specifying exact byte offset position of the string you | ||
14 | * are looking for within a public key. When offset is negative, the program | ||
15 | * just looks for the desired string being somewhere, doesn't matter where, in | ||
16 | * the public key. | ||
17 | * | ||
18 | * String - a hex string that you want to have in your public key. It must have | ||
19 | * an even number of letters, since every two hexes map to a single byte of | ||
20 | * the public key. | ||
21 | * | ||
22 | * Examples: | ||
23 | * strkey 0 0123 | ||
24 | * Looks for a public key that begins with "0123". | ||
25 | * | ||
26 | * strkey 1 0123 | ||
27 | * Looks for a public key that has "0123" starting at its second byte, i.e. "XX0123...". | ||
28 | * | ||
29 | * strkey 2 0123 | ||
30 | * Looks for a public key that has "0123" starting at its third byte, i.e. "XXXX0123...". | ||
31 | * (each two hexes represent a single byte of a public key) | ||
32 | * | ||
33 | * strkey -1 AF57CC | ||
34 | * Looks for a public key that contains "AF57CC", regardless of its position. | ||
35 | * | ||
36 | * To compile with gcc and sodium: gcc strkey.c -o strkey -lsodium | ||
37 | */ | ||
38 | |||
39 | #include <stdio.h> | ||
40 | #include <string.h> | ||
41 | |||
42 | #include <sodium.h> | ||
43 | |||
44 | #define PRINT_TRIES_COUNT | ||
45 | |||
46 | void print_key(unsigned char *key) | ||
47 | { | ||
48 | size_t i; | ||
49 | for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++) { | ||
50 | if (key[i] < 16) { | ||
51 | fprintf(stdout, "0"); | ||
52 | } | ||
53 | |||
54 | fprintf(stdout, "%hhX", key[i]); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | int main(int argc, char *argv[]) | ||
59 | { | ||
60 | unsigned char public_key[crypto_box_PUBLICKEYBYTES + 1]; // null terminator | ||
61 | unsigned char secret_key[crypto_box_SECRETKEYBYTES]; | ||
62 | int offset = 0; | ||
63 | size_t len; | ||
64 | unsigned char desired_bin[crypto_box_PUBLICKEYBYTES + 1]; // null terminator | ||
65 | |||
66 | if (argc == 3) { | ||
67 | offset = atoi(argv[1]); | ||
68 | char *desired_hex = argv[2]; | ||
69 | len = strlen(desired_hex); | ||
70 | if (len % 2 != 0) { | ||
71 | fprintf(stderr, "Desired key should have an even number of letters\n"); | ||
72 | exit(1); | ||
73 | } | ||
74 | size_t block_length = (offset < 0 ? 0 : offset) + len/2; | ||
75 | if (block_length > crypto_box_PUBLICKEYBYTES) { | ||
76 | fprintf(stderr, "The given key with the given offset exceed public key's length\n"); | ||
77 | exit(1); | ||
78 | } | ||
79 | |||
80 | // convert hex to bin | ||
81 | char *pos = desired_hex; | ||
82 | size_t i; | ||
83 | for (i = 0; i < len; pos += 2) { | ||
84 | sscanf(pos, "%2hhx", &desired_bin[i]); | ||
85 | ++i; | ||
86 | } | ||
87 | } else { | ||
88 | fprintf(stdout, "Usage: executable <byte offset> <desired hex string with even number of letters>\n"); | ||
89 | exit(1); | ||
90 | } | ||
91 | |||
92 | desired_bin[len/2] = '\0'; | ||
93 | public_key[crypto_box_PUBLICKEYBYTES + 1] = '\0'; | ||
94 | |||
95 | #ifdef PRINT_TRIES_COUNT | ||
96 | long long unsigned int tries = 0; | ||
97 | #endif | ||
98 | |||
99 | if (offset < 0) { | ||
100 | do { | ||
101 | #ifdef PRINT_TRIES_COUNT | ||
102 | tries ++; | ||
103 | #endif | ||
104 | crypto_box_keypair(public_key, secret_key); | ||
105 | } while (strstr(public_key, desired_bin) == 0); | ||
106 | } else { | ||
107 | unsigned char *p = public_key + offset; | ||
108 | len /= 2; | ||
109 | do { | ||
110 | #ifdef PRINT_TRIES_COUNT | ||
111 | tries ++; | ||
112 | #endif | ||
113 | crypto_box_keypair(public_key, secret_key); | ||
114 | } while (memcmp(p, desired_bin, len) != 0); | ||
115 | } | ||
116 | |||
117 | fprintf(stdout, "Public key: "); | ||
118 | print_key(public_key); | ||
119 | fprintf(stdout, "\n"); | ||
120 | |||
121 | fprintf(stdout, "Private key: "); | ||
122 | print_key(secret_key); | ||
123 | fprintf(stdout, "\n"); | ||
124 | |||
125 | #ifdef PRINT_TRIES_COUNT | ||
126 | fprintf(stdout, "Found the key pair on %llu try.\n", tries); | ||
127 | #endif | ||
128 | |||
129 | return 0; | ||
130 | } \ No newline at end of file | ||