#!/usr/bin/env txrlisp ;; One-Clause BSD License ("1BSD") ;; ;; Copyright 2022 Kaz Kylheku ;; ;; 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. (defvarl vowels '#"a i u e o") (defvarl diphthongs '#"ya yu yo") (defvarl consonants '#"k g s z t d n h p b m r") (defvarl symbols '#"! # @ $ % ^ & * ? /") (defvarl mora-fix (let ((trie (make-trie))) (mapdo (op trie-add trie) '#"si sy zi zy ti tu ty di du dy hu" '#"shi sh ji j chi tsu ch ji zu j fu") trie)) (defvar mora (flow (append vowels diphthongs '#"wa" [maprod join consonants vowels] [maprod join consonants diphthongs]) (filter-string-tree mora-fix) uniq)) (defun choose (list index) [list (mod index (len list))]) (defun jp-hash (phrase) (let* ((buf (sha256 phrase)) (word (carray-buf buf (ffi le-uint16))) (ms (mapcar (opip word (choose mora)) 0..6)) (dig (list (pic "#" (mod [word 6] 10)))) (sym (list (choose symbols [word 7])))) (upd [ms 0] copy) (upd [[ms 0] 0] chr-toupper) (cat-str (caseq (logand [word 8] 7) (0 (append [ms 0..3] sym [ms 3..6] dig)) (1 (append sym [ms 0..3] dig [ms 3..6])) (2 (append [ms 0..2] sym [ms 2..4] dig [ms 4..6])) (3 (append [ms 0..2] dig [ms 2..4] sym [ms 4..6])) (4 (append [ms 0..3] '#"n" sym [ms 3..6] dig)) (5 (append sym [ms 0..3] dig [ms 3..6] '#"n")) (6 (append [ms 0..2] '#"n" sym [ms 2..4] dig [ms 4..6])) (7 (append [ms 0..2] dig [ms 2..4] sym [ms 4..6] '#"n")))))) (compile-only (match-case *args* ((@arg) (put-line (jp-hash arg))) (@else (put-line `@{self-path}: one argument required`))))