-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodename.clj
More file actions
90 lines (76 loc) · 2.51 KB
/
codename.clj
File metadata and controls
90 lines (76 loc) · 2.51 KB
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
(ns codename
(:require [clojure.string :as string]
[diceware :as diceware]
[fileinfo :as fileinfo])
(:import
[System
Console]
[System.IO
Stream
MemoryStream
StreamReader
StreamWriter]
[System.Security.Cryptography
HashAlgorithm])
(:gen-class))
(def algorithm-version "0")
(def usage-string (.ToString #"
codename.exe reads from STDIN a list of lines and generates
a pseudo-version with a (hopefully) pronouncable name.
The codename is based on the set of lines, independent of
line endings, capitalization, and leading and trailing whitespace.
# Example 1
Generate a name for the set of *.exe *.dll files in the current directory
dir /b *.exe *.dll|codename.exe
# Example 2
Generate a name for the scpview.exe and its dependencies.
deps.exe program.exe|FileInfo --sha1|codename.exe
"))
(defn- stderr! [& args]
(binding [*out* *err*]
(apply println args)))
(defn get-hash-content-lines-version-0
"Gets a string containing the sorted, normalized,
lines. Each line is normalized by trimming and case-converting to lower.
Each line is followed by \\r\\n."
[lines]
(->> lines
(filter (complement string/blank?))
(map (comp string/lower-case string/trim))
sort))
(defn join-hash-content-lines
"Each line is followed by \\r\\n."
[lines]
(->> lines
(#(mapcat vector % (repeat "\r\n")))
(string/join)))
(defn string->stream [s]
(let [stream (MemoryStream.)
writer (doto (StreamWriter. stream)
(.Write s)
(.Flush))]
(.set_Position stream 0)
stream))
(defn calculate-hash-bytes [content]
(let [^HashAlgorithm hasher (HashAlgorithm/Create "SHA1")
^Stream stream (string->stream content)]
(.ComputeHash hasher stream)))
(defn run [lines]
(let [content-lines (get-hash-content-lines-version-0 lines)
content (join-hash-content-lines content-lines)
bytes (calculate-hash-bytes content)
words (fileinfo/bytes->dice-words bytes)
hex (fileinfo/hexadecimal bytes)]
(println (str "codename:\t" algorithm-version "_" words))
(println (str "sha1: \t" hex))
(println)
(println "-----Begin" (count content-lines) "Lines of Content-----")
(print content)
(println "-----End Content-----")
(println)
(println "Generated by codename algorithm version" algorithm-version)))
(defn -main[& args]
(if (some #{"/?" "-?" "--help"} args)
(stderr! usage-string)
(run
(line-seq (Console/get_In)))))