aboutsummaryrefslogtreecommitdiffstats
path: root/jp-hash.c
blob: d4bb4d55b8b4a28de328cb2b530df2675cf69baa (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * One-Clause BSD License ("1BSD")
 *
 * Copyright 2022  Kaz Kylheku <kaz@kylheku.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following condition is
 * met:
 *
 * 1. The source code distribution retains the above copyright notice,
 *    this condition, and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef JP_HASH_H
#define JP_HASH_H

#ifdef __cplusplus
extern "C" {
#endif

void jp_hash(char *out, const char *in);

#ifdef __cplusplus
}
#endif

#if JP_HASH_IMPL

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <openssl/sha.h>

#define elof(array) ((int)(sizeof(array)/sizeof(array[0])))

static const char *mora[] = {
 "a", "i", "u", "e", "o", "ya", "yu", "yo", "wa",
 "ka", "ki", "ku", "ke", "ko", "ga", "gi", "gu", "ge", "go",
 "sa", "shi", "su", "se", "so", "za", "ji", "zu", "ze", "zo",
 "ta", "chi", "tsu", "te", "to", "da", "de", "do",
 "na", "ni", "nu", "ne", "no", "ha", "hi", "fu", "he", "ho",
 "pa", "pi", "pu", "pe", "po", "ba", "bi", "bu", "be", "bo",
 "ma", "mi", "mu", "me", "mo", "ra", "ri", "ru", "re", "ro",
 "kya", "kyu", "kyo", "gya", "gyu", "gyo", "sha", "shu", "sho",
 "ja", "ju", "jo", "cha", "chu", "cho", "nya", "nyu", "nyo",
 "hya", "hyu", "hyo", "pya", "pyu", "pyo", "bya", "byu", "byo",
 "mya", "myu", "myo", "rya", "ryu", "ryo"
};

static const char *digit[] = {
  "0", "1", "2", "3", "4",
  "5", "6", "7", "8", "9"
};

static const char *symbol[] = {
  "!", "#", "@", "$", "%",
  "^", "&", "*", "?", "/"
};

void jp_hash(char *out, const char *in)
{
  unsigned char hash[32];
  uint16_t word[9];
  const char *ms[6];
  const char *sym;
  const char *dig;
  char *ms0;
  int i;

  SHA256((const unsigned char *) in, strlen(in), hash);

  for (i = 0; i < elof(word); i++)
    word[i] = hash[2*i] | (((uint16_t) hash[2*i + 1]) << 8);

  for (i = 0; i < elof(ms); i++)
    ms[i] = mora[word[i] % elof(mora)];

  dig = digit[word[6] % elof(digit)];
  sym = symbol[word[7] % elof(symbol)];

  ms0 = strdup(ms[0]);
  ms0[0] = toupper((unsigned char) ms0[0]);
  ms[0] = ms0;

  switch (word[8] & 7) {
  case 0:
    snprintf(out, 32, "%s%s%s%s%s%s%s%s",
             ms[0], ms[1], ms[2], sym,
             ms[3], ms[4], ms[5], dig);
    break;
  case 1:
    snprintf(out, 32, "%s%s%s%s%s%s%s%s",
             sym, ms[0], ms[1], ms[2],
             dig, ms[3], ms[4], ms[5]);
    break;
  case 2:
    snprintf(out, 32, "%s%s%s%s%s%s%s%s",
             ms[0], ms[1], sym, ms[2],
             ms[3], dig, ms[4], ms[5]);
    break;
  case 3:
    snprintf(out, 32, "%s%s%s%s%s%s%s%s",
             ms[0], ms[1], dig, ms[2],
             ms[3], sym, ms[4], ms[5]);
    break;
  case 4:
    snprintf(out, 32, "%s%s%sn%s%s%s%s%s",
             ms[0], ms[1], ms[2], sym,
             ms[3], ms[4], ms[5], dig);
    break;
  case 5:
    snprintf(out, 32, "%s%s%s%s%s%s%s%sn",
             sym, ms[0], ms[1], ms[2],
             dig, ms[3], ms[4], ms[5]);
    break;
  case 6:
    snprintf(out, 32, "%s%sn%s%s%s%s%s%s",
             ms[0], ms[1], sym, ms[2],
             ms[3], dig, ms[4], ms[5]);
    break;
  case 7:
    snprintf(out, 32, "%s%s%s%s%s%s%s%sn",
             ms[0], ms[1], dig, ms[2],
             ms[3], sym, ms[4], ms[5]);
    break;
  }

  free(ms0);
}

#if JP_HASH_MAIN

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

  if (argc != 2) {
    if (argc)
      fprintf(stderr, "%s: one argument required\n", argv[0]);
    return EXIT_FAILURE;
  }

  jp_hash(jph, argv[1]);
  puts(jph);
  return 0;
}

#endif // JP_HASH_MAIN
#endif // JP_HASH_IMPL
#endif // JP_HASH_H