aboutsummaryrefslogtreecommitdiff
path: root/shabottom.c
blob: 10100c9a73a0253a12c213e730d677c3c20d5f5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* crypto.c
 *
 * Copyright 2024 axtlos <axtlos@disroot.org>
 *
 * 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, version 3.
 *
 * 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 <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include <stdint.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

const char start[10] = {'s','s','s','d','d','d','f','f','f','f'};
const char end[11] = {'g','g','g','h','h','h','j','k','l','l','l'};

unsigned int
get_data(char *file)
{
  unsigned int ret = 0;
  FILE *data = NULL;
  if (file) {
    data = fopen (file, "r");
  } else {
    data = stdin;
  }
  unsigned int character;
  while ((character = fgetc (data)) != EOF) {
    ret = ret + character;
  }
  return ret;
}

int
main (int argc, char *argv[]) {

  char *keymash = malloc(33);
  int r,total = 0;
  char prev = 'a';
  int val = 0;
  int switchup = 0;

  int from_stdin = argc > 1 ? 1 : 0;
  unsigned int data = 0;
  data = get_data (from_stdin == 0 ? NULL : argv[argc-1]);
  srand(data);
  total += sprintf(keymash+total, "%c", prev);
  while (total < 32) {
    if (val == 0) {
      int j = total+2 < 32 ? 2 : 1;
      for (int i = 0; i < j; i++) {
	r = abs((rand() % 10 - r) + r);
	if (prev != start[r]) {
	  total += sprintf(keymash+total, "%c", start[r]);
	  prev = start[r];
	} else {
	  i -= 1;
	}
      }
      switchup = switchup < 0 ? 0 : switchup+1;
    } else if (val == 1) {
      int j = total+2 < 32 ? 2 : 1;
      for (int i = 0; i < j; i++) {
	r = abs((rand() % 11));
	if (prev != end[r]) {
	  total += sprintf(keymash+total, "%c", end[r]);
	  prev = end[r];
	} else {
	  i -= 1;
	}
      }
      switchup = switchup > 0 ? 0 : switchup-1;
    }
    if (switchup < 2 && switchup > -2) {
      val = abs(rand() % 2);
    } else {
      val = switchup < 0 ? 0 : 1;
    }
  }
  puts(keymash);
  free(keymash);
  return 0;

}