diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..61a3b88 --- /dev/null +++ b/src/main.c @@ -0,0 +1,145 @@ +// upwgen generates random internationalized passwords +// Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +#include <stdlib.h> +#include <stdio.h> +#include <time.h> +#include <locale.h> + + +// intrcat: +// Integer range concatenate. Appends the specified integer range to an int +// array. +// +// @arr Array to cat range of ints to +// @rstart Range start integer +// @rend Range end integer +// +// @return Number of integers appended to array +int intrcat(unsigned int* arr, unsigned int rstart, unsigned int rend) { + int i = 0; + int total = rend - rstart; // Calculate our return count + + while(arr[i] != '\0') + i++; + + while(rstart <= rend) { + arr[i] = rstart; + //printf("% -4d % -7d %lc\n", i, rstart, rstart); + rstart++; + i++; + } + + arr[i] = '\0'; + return total; +} + + +// populate_intl_arr: +// Populates an unsigned integer array with common unicode (utf-8) language +// alphabets and symbols. +// +// Some example unicode integer ranges: +// 33 - 126 Standard english ascii +// 256 - 383 Latin extended A block +// 256 - 383 Latin extended B block +// 913 - 969 Greek +// 1040 - 1103 Russian +// 1329 - 1414 Armenian +// 1488 - 1514 Hebrew +// 65166 - 65265 Arabic +// +// No/rare font support (boo!) +// 2325 - 2373 Devanagari (Hindi) +// 2437 - 2509 Bengali alphabet +// 2949 - 3020 Tamil +// 3585 - 3663 Thai +// 5792 - 5880 Runic +// 11392 - 11483 Coptic alphabet +// 66560 - 66639 Deseret +// +// @out Unsigned int array to be populated. +// +// @return Size of the array contents +int populate_intl_arr(unsigned int* out) { + out[0] = '\0'; + int count = 0; + + // Populate the array + count += intrcat(out, 33, 126); // English + count += intrcat(out, 256, 383); // Latin A block + count += intrcat(out, 399, 691); // Latin B block + count += intrcat(out, 913, 969); // Greek + count += intrcat(out, 1040, 1103); // Russian + count += intrcat(out, 1329, 1414); // Armenian + count += intrcat(out, 1488, 1514); // Hebrew + count += intrcat(out, 65166, 65265); // Arabic + + return count; +} + + +// print_intl_arr: +// Prints array containing unsigned ints representing internal characters. +// Outputs to STDOUT the unicode decimal, followed by the unicode character. +// +// @arr Unicode array to print +void print_intl_arr(unsigned int* arr) { + int i = 0; // cursor + + while(arr[i] != '\0') { + printf("%5d: [%lc]\n", arr[i], arr[i]); + i++; + } +} + + +int main(int argc, char* argv[]) { + char* passlen; // Argv 1, user specified length of the password + int count; // Number of international chars to choose from + struct timespec ts; // Timespec for seeding rng + int len; // Password length + unsigned long seed; // Seed for the RNG (current seconds * nanoseconds) + unsigned int chars[1024]; // Uint array to hold international chars + + setlocale(LC_ALL, "en_US.UTF-8"); + + if(argc == 1) { + printf("Password length required\n"); + return 1; + } + + passlen = argv[1]; + + // Store supported chars here! + count = populate_intl_arr(chars); + //printf("%d international chars for use\n", count); + //print_intl_arr(chars); + //return 0; + + clock_gettime(CLOCK_REALTIME, &ts); + seed = ts.tv_sec + ts.tv_nsec; + srand((unsigned)seed); + + len = atoi(passlen); + while(len > 0) { + int r = rand() % count; + printf("%lc", chars[r]); + len--; + } + printf("\n"); + + return 0; +} |