Shell脚本生成包含非重复字符的单词列表

时间:2018-02-24 22:34:59

标签: shell lisp words

我找到了一个lisp程序。它有效,但不完全是我需要的。输出如下:

class foo
{
public:
    foo()
    {
        a = std::make_unique<T>();
        a->something();
        a->other_thing();

        b = std::make_unique<T>();
        b->other_thing(*a);
    }

private:
    std::unique_ptr<T> a;     
    std::unique_ptr<T> b; 
};

我需要创建一个单词列表,使用Base32字符,长度为16个字符,单词中包含非重复字符。 然后我需要在单词中添加2323232323235ve3 2323232323235ve4 2323232323235ve5 2323232323235ve6

.txt

然后我需要SHA512这个词并检查已知的哈希。

是否可以输出与已知哈希匹配的单词?

这是LISP来源

asdfjklwert7csui.txt
jcfklinesftw8se3.txt

Bash Script输出到文件,看起来像。我需要输出为小写。

#!/usr/bin/clisp

(defparameter *character-set* "234567abcdefghijklmnopqrstuvwxyz")
;(defparameter *character-set* "ABC")     ; < --- this line is for testing

(defparameter *word-length* 16)
;(defparameter *word-length* 4)           ; < --- this line is for testing

(defparameter *character-list*
       (coerce *character-set* 'list))

(defun final-char (in-string)
   (cond
      ((> (length in-string) 0)
         (elt in-string (1- (length in-string))))
      (t
         nil)))

(defun new-char-list (in-string)
   (let ((result))
      (mapcar
         (lambda (candidate)
            (cond
               ((not (eql candidate (final-char in-string)))
                  (push candidate result))))
         *character-list*)
      (nreverse result))
      )

(defun extend-string (in-string desired-length)
   (mapcar
      (lambda (new-char)
         (let ((new-string (concatenate 'string in-string (string new-char))))
            (cond
               ((>  (length new-string) desired-length))
               ((>= (length new-string) desired-length)
                  (format t "~a~%" new-string))
               (t
                  (extend-string new-string desired-length)))))
      (new-char-list in-string)))

(extend-string "" *word-length*)

这是Bash脚本

K5SMKLK5W85T6GTC
RZJRNV0VO1LVIMEM
RPSW59OPQLUBJKC5

1 个答案:

答案 0 :(得分:1)

这是一个使用SBCL测试的Common Lisp答案。 由于您需要计算哈希值,因此我将使用名为Ironclad的外部库。首先要安装它 Install Quicklisp。然后:

(ql:quickload :ironclad)

这部分可以定制:

(defparameter *character-set* "234567abcdefghijklmnopqrstuvwxyz")
(defparameter *suffix* ".txt")

辅助功能

现在,我们将映射所有可能符合您的约束的字符串(没有相同的连续字符)。我们也将把这些字符串作为字节来操作,因为Ironclad只从字节向量计算哈希值。不需要分配这么多字符串,只需重复使用相同的缓冲区:

(defun make-buffer (size)
  (concatenate '(vector (unsigned-byte 8))
               (make-array size :element-type '(unsigned-byte 8))
               (ironclad:ascii-string-to-byte-array *suffix*)))

上面分配了所需的字节向量,考虑了后缀,转换为字节。 下面,我们将对字符集执行相同操作,该字符集也被强制为列表(以便能够使用DOLIST):

(defun make-character-set ()
  (coerce (ironclad:ascii-string-to-byte-array *character-set*)
          'list))

我们还希望能够将哈希字符串转换为字节向量,但也可以直接接受向量。以下函数确保将给定值转换为所需类型:

(defun ensure-hash (hash-designator)
  (etypecase hash-designator
    (string (ironclad:hex-string-to-byte-array hash-designator))
    (vector (coerce hash-designator '(vector (unsigned-byte 8))))))

查找哈希

现在,我们可以找到给定一组生成的单词的哈希值。 SIZE参数表示后缀前面有多少个字符,HASH-DESIGNATOR是十六进制表示法的字符串,或者是字节向量:

(defun find-hash (size hash-designator)
  (let ((hash (ensure-hash hash-designator))
        (buffer (make-buffer size))
        (character-set (make-character-set)))
    (labels ((level (depth forbidden)
               (cond
                 ((>= depth size)
                  (when (equalp hash (ironclad:digest-sequence
                                      'ironclad:sha512 buffer))
                    (return-from find-hash
                      (values (map 'string #'code-char buffer)
                              buffer))))
                 (t (let ((next (1+ depth)))
                      (dolist (c character-set)
                        (unless (= c forbidden)
                          (setf (aref buffer depth) c)
                          (level next c))))))))
      (level 0 0))))

本地level函数的一般情况包括根据字符集在缓冲区中的位置depth设置字符,并忽略禁用字符,这是最后设置的字符(或者最初为零)。当level到达size时,我们将单词存储在缓冲区中作为字节向量。在这种情况下,我们对该单词进行散列并将其与所需的散列进行比较。如果匹配,我们将字节数组(字符代码)转换为字符串,并返回内部缓冲区(它已经计算好,也许可以重复使用)。

实施例

(find-hash 3 "ddd2379f9a1adf4f0afa0befafdb070fb942d4d4e0331a31d43494149307221e5e699da2a08f59144b0ed415dea6f920cf3dab8ca0b740d874564d83b9b6f815")
=> "zyc.txt"
   #(122 121 99 46 116 120 116)
然而,由于指数的复杂性,这个任务对16个字符来说是不切实际的:

> (time (find-hash 4 #(0)))
Evaluation took:
  1.679 seconds of real time
  1.676000 seconds of total run time (1.672000 user, 0.004000 system)
  [ Run times consist of 0.028 seconds GC time, and 1.648 seconds non-GC time. ]
  99.82% CPU
  4,019,832,288 processor cycles
  899,920,096 bytes consed

NIL

> (time (find-hash 5 #(0)))
Evaluation took:
  51.768 seconds of real time
  51.796000 seconds of total run time (51.684000 user, 0.112000 system)
  [ Run times consist of 0.952 seconds GC time, and 50.844 seconds non-GC time. ]
  100.05% CPU
  123,956,130,558 processor cycles
  27,897,672,624 bytes consed