fork download
  1. /* ==========================================================================
  2.  * Program : keygen
  3.  * Author : migf1
  4.  * Language : C (C99)
  5.  * Description : a simple, slow & and rather unsafe generator of unique keys
  6.  * Source files : keygen.c
  7.  * gcc options : -std=c99
  8.  * CmdLn Usage : keygen [ >fname.txt ]
  9.  *
  10.  * Extra Notes :
  11.  *
  12.  * In the source code, you may easily...
  13.  * - change the size and the total count of the generated keys
  14.  * - exclude/include any of the available char-sets from the key generation process
  15.  * - demand at least 1 char from any of the available char-sets to be present
  16.  * in every generated key (even if the char-set is generally excluded )
  17.  *
  18.  * ==========================================================================
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdbool.h>
  24. #include <string.h>
  25. #include <time.h>
  26.  
  27. #define KEYSIZE 10+1 // μέγεθος κλειδιού
  28. #define NKEYS 15 // πλήθος κλειδιών
  29.  
  30. // Bitflag Options for the program (do not touch them, unless you are a programer)
  31. #define DEMAND_LOWER (1 << 0) // 0x01
  32. #define DEMAND_UPPER (1 << 1) // 0x02
  33. #define DEMAND_DIGITS (1 << 2) // 0x04
  34. #define DEMAND_SPECL (1 << 3) // 0x08
  35. #define USE_LOWER (1 << 4) // 0x10
  36. #define USE_UPPER (1 << 5) // 0x20
  37. #define USE_DIGITS (1 << 6) // 0x40
  38. #define USE_SPECL (1 << 7) // 0x80
  39.  
  40. // Available Char-Sets (you may freely add or remove chars in any of those sets)
  41. #define POOL_LOWER "abcdefghijklmnopqrstuvwxyz"
  42. #define POOL_UPPER "ABCDEGFHIJKLMNOPQRSTUVWXYZ"
  43. #define POOL_DIGITS "0123456789"
  44. #define POOL_SPECL "/?.,><|!@#$%^&*()_+=-~;:][}{ \\\""
  45.  
  46. // -------------------------------------------------------------------------------------
  47. // Return true if the cstring keys[ istop ] is not already contained between the
  48. // 0th and the (istop-1)th element of the keys[] array of cstrings,
  49. // otherwise return false
  50. //
  51. _Bool unique_key( const int istop, char keys[ NKEYS ][ KEYSIZE ] )
  52. {
  53. register int ikey = 0;
  54.  
  55. for (; ikey < istop; ikey++)
  56. if ( strcmp( keys[ikey], keys[istop] ) == 0 )
  57. return false;
  58.  
  59. return true;
  60. }
  61. // -------------------------------------------------------------------------------------
  62. // This function makes sure that requested demands for keys to contain
  63. // at least 1 char from any of the available char-sets are fulfilled
  64. //
  65. void apply_demanded( const unsigned char demand, char *key, const size_t keylen )
  66. {
  67. int i; char c;
  68. _Bool iused[ keylen ]; // map for used i (pos)
  69. memset(iused, false, keylen); // zero the map
  70.  
  71. /*
  72. * if demanded, put a LOWER-case letter inside the key
  73. */
  74. if ( demand & DEMAND_LOWER )
  75. {
  76. c = POOL_LOWER[ rand() % strlen(POOL_LOWER) ]; // random LOWR letter
  77. i = rand() % keylen; // random pos in key
  78. key[ i ] = c; // put letter at pos
  79. iused[ i ] = true; // mark pos as used
  80. }
  81.  
  82. /*
  83. * if demanded, put an UPPER-case letter inside the key
  84. */
  85. if ( demand & DEMAND_UPPER )
  86. {
  87. c = POOL_UPPER[ rand() % strlen(POOL_UPPER) ]; // random UPPR letter
  88. do {
  89. i = rand() % keylen; // random pos in key
  90. } while( iused[ i ] == true ); // make sure it's unused
  91.  
  92. key[ i ] = c; // put letter at pos
  93. iused[ i ] = true; // mark pos as used
  94. }
  95.  
  96. /*
  97. * if demanded, put a DIGIT inside the key
  98. */
  99. if ( demand & DEMAND_DIGITS )
  100. {
  101. c = POOL_DIGITS[ rand() % strlen(POOL_DIGITS) ];// random digit
  102. do {
  103. i = rand() % keylen; // random pos in key
  104. } while( iused[ i ] == true ); // make sure it's unused
  105.  
  106. key[ i ] = c; // put digit at pos
  107. iused[ i ] = true; // mark pos as used
  108. }
  109.  
  110. /*
  111. * if demanded, put a SPECIAL CHAR inside the key
  112. */
  113. if ( demand & DEMAND_SPECL)
  114. {
  115. c = POOL_SPECL[ rand() % strlen(POOL_SPECL) ]; // random special char
  116. do {
  117. i = rand() % keylen; // random pos in key
  118. } while( iused[ i ] == true ); // make sure it's unused
  119.  
  120. key[ i ] = c; // put digit at pos
  121. iused[ i ] = true; // mark pos as used
  122. }
  123.  
  124. return;
  125. }
  126. // -------------------------------------------------------------------------------------
  127. // Calc and return the appropriate size for the pool, based on the values of the USE_
  128. // bits contained in: options
  129. //
  130. size_t pool_calcsize( const unsigned char options )
  131. {
  132. size_t ret = 0;
  133.  
  134. if ( options & USE_LOWER ) // make room for lower-case letters
  135. ret += strlen( POOL_LOWER );
  136.  
  137. if ( options & USE_UPPER ) // make room for upper-case letters
  138. ret += strlen( POOL_UPPER );
  139.  
  140. if ( options & USE_DIGITS ) // make room for digits
  141. ret += strlen( POOL_DIGITS );
  142.  
  143. if ( options & USE_SPECL ) // make room for special chars
  144. ret += strlen( POOL_SPECL );
  145.  
  146. return ++ret; // make room for '\0' at the end
  147. }
  148. // -------------------------------------------------------------------------------------
  149. // Initialize the chars pool, based on the values of the USE_ bits contained in: options.
  150. // Return the initialized pool (which is as a cstring)
  151. //
  152. char *pool_init( char *pool, const size_t poolsize, const unsigned char options )
  153. {
  154. if ( !pool )
  155. return NULL;
  156.  
  157. memset( pool, 0, poolsize ); // clear the pool
  158.  
  159. if ( options & USE_LOWER ) // include lower-case letters
  160. strncat( pool, POOL_LOWER, poolsize-1 );
  161.  
  162. if ( options & USE_UPPER ) // include upper-case letters
  163. strncat( pool, POOL_UPPER, poolsize-1 );
  164.  
  165. if ( options & USE_DIGITS ) // include digits
  166. strncat( pool, POOL_DIGITS, poolsize-1 );
  167.  
  168. if ( options & USE_SPECL ) // include special chars
  169. strncat( pool, POOL_SPECL, poolsize-1 );
  170.  
  171. return pool;
  172. }
  173. // -------------------------------------------------------------------------------------
  174. int main( void )
  175. {
  176. /*
  177. * To disable any of the following options, delete or comment-out its
  178. * corresponding line below.
  179. *
  180. * USE_ options specify whether you want chars from that particular
  181. * char-set to participate in the key generation process or not
  182. *
  183. * DEMAND_ options specify whether yoy want at least 1 char of that
  184. * particular char-set to be included in every generated key or not
  185. *
  186. * Note that you can disable a USE_ option without disabling its
  187. * corresponding DEMAND_ option. In such cases, the disabled char-set
  188. * does not generally participate in the generation process, but 1
  189. * of its chars (picked randomly) will be present inside every key,
  190. * at a random position.
  191. *
  192. * To completely prevent a char-set from participating in the
  193. * the generation process, delete both its USE_ and its DEMAND_ option
  194. */
  195. unsigned char options = // bit-flagged byte
  196. DEMAND_LOWER // ... demand lower letter
  197. + DEMAND_UPPER // ... demand upper letter
  198. + DEMAND_DIGITS // ... demand digit
  199. + DEMAND_SPECL // ... demand special char
  200. + USE_LOWER // ... use lower letters
  201. + USE_UPPER // ... use upper letters
  202. + USE_DIGITS // ... use digits
  203. + USE_SPECL // ... use special chars
  204. ;
  205. const size_t POOLSIZE = pool_calcsize(options); // calc the size of chars pool
  206. char pool[ POOLSIZE ]; // the pool itself (cstring)
  207. char keys[ NKEYS ][ KEYSIZE ] = { {0, 0} }; // array of (cstring) keys
  208. int i=0, ikey=0; // i for chars, ikey for keys
  209.  
  210. srand( time(NULL) ); // init the pseudo-random seed
  211.  
  212. pool_init( pool, POOLSIZE, options ); // initialize the chars pool
  213.  
  214. for (ikey=0; ikey < NKEYS; ikey++ ) // generate & store keys
  215. { // into the keys[NKEYS] array
  216. do { // ...
  217. for (i=0; i < KEYSIZE-1; i++) // ... generate a key
  218. keys[ ikey ][ i ] = pool[ rand() % (POOLSIZE-1) ];
  219. } while ( !unique_key(ikey, keys) ); // ... make sure it is unique
  220.  
  221. // apply any char demands
  222. apply_demanded( options, keys[ikey], KEYSIZE-1 );
  223.  
  224. puts( keys[ikey] ); // print the generated key
  225. }
  226.  
  227. return 0;
  228. }
  229.  
Success #stdin #stdout 0s 1720KB
stdin
Standard input is empty
stdout
6iD@qrS#R,
0mde%JQiNY
.gQF\8B%>t
52FCiXph(,
a!^~7w)D{-
KG^sWMy0!1
-BT)>9_TxO
dt|@F+r/7G
n9z~HM]r]R
+<64Ng@$^H
T7?7NBFtr_
YtL3GXb+Jr
hH#(|zGM"7
e#~eY/:2!t
5-SZ"R[x]G