Skip to content

Commit 375d8b4

Browse files
committed
added crc64, md5, sha1, sha256
1 parent 1858e93 commit 375d8b4

File tree

3 files changed

+173
-8
lines changed

3 files changed

+173
-8
lines changed

profiles.clj

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
{:dev {:global-vars {*warn-on-reflection* true}
1+
{:dev {:global-vars {*warn-on-reflection* true}
22

3-
:source-paths #{"src"}
4-
:resource-paths ["resources"]
3+
:source-paths #{"src-clj"}
4+
:java-source-paths #{"src-java"}
5+
:resource-paths ["resources"]
56

6-
:target-path "target/%s"
7-
:clean-targets ^{:protect false} [:target-path]
7+
:jar-exclusions [#"\.java"]
88

9-
:plugins [[org.apache.maven.wagon/wagon-ssh-external "3.4.3"]
10-
[org.apache.maven.wagon/wagon-http-lightweight "3.4.3"]]}
9+
:target-path "target/%s"
10+
:clean-targets ^{:protect false} [:target-path]
11+
12+
:dependencies [[commons-codec "1.15"]]
13+
14+
:plugins [[org.apache.maven.wagon/wagon-ssh-external "3.4.3"]
15+
[org.apache.maven.wagon/wagon-http-lightweight "3.4.3"]]}
1116

1217
:provided {:dependencies [[org.clojure/clojure "1.10.3"]]}
1318

14-
:jar {:aot :all}}
19+
:jar {:aot :all}}

src-clj/zlib_tiny/core.clj

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
(ns zlib-tiny.core
2+
(:import (java.util.zip InflaterInputStream
3+
GZIPInputStream
4+
GZIPOutputStream
5+
DeflaterInputStream
6+
Inflater
7+
ZipException
8+
CRC32)
9+
(zlib_tiny CRC64)
10+
(org.apache.commons.io IOUtils)
11+
(java.security MessageDigest)
12+
(java.io ByteArrayInputStream
13+
ByteArrayOutputStream
14+
BufferedInputStream
15+
InputStream)))
16+
17+
(defn str->bytes
18+
"Returns the encoding's bytes corresponding to the given string. If no
19+
encoding is specified, UTF-8 is used."
20+
[^String s & [^String encoding]]
21+
(.getBytes s (or encoding "UTF-8")))
22+
23+
(defn bytes->str
24+
"Returns the String corresponding to the given encoding's decoding of the
25+
given bytes. If no encoding is specified, UTF-8 is used."
26+
[^bytes b & [^String encoding]]
27+
(String. b (or encoding "UTF-8")))
28+
29+
(defn gunzip
30+
"Returns a gunzip'd version of the given byte array."
31+
[b]
32+
(when b
33+
(cond
34+
(instance? InputStream b)
35+
(GZIPInputStream. b)
36+
:else
37+
(IOUtils/toByteArray (GZIPInputStream. (ByteArrayInputStream. b))))))
38+
39+
(defn gzip
40+
"Returns a gzip'd version of the given byte array."
41+
[b]
42+
(when b
43+
(let [baos (ByteArrayOutputStream.)
44+
gos (GZIPOutputStream. baos)]
45+
(IOUtils/copy (ByteArrayInputStream. b) gos)
46+
(.close gos)
47+
(.toByteArray baos))))
48+
49+
(defn force-byte-array
50+
"force b as byte array if it is an InputStream, also close the stream"
51+
^bytes [b]
52+
(if (instance? InputStream b)
53+
(try (IOUtils/toByteArray ^InputStream b)
54+
(finally (.close ^InputStream b)))
55+
b))
56+
57+
(defn inflate
58+
"Returns a zlib inflate'd version of the given byte array or InputStream."
59+
[b]
60+
(when b
61+
;; This weirdness is because we don't know about what kind of deflation
62+
;; sender using, so we try one way, then if that doesn't work, reset and
63+
;; try the other way
64+
(let [stream (BufferedInputStream. (if (instance? InputStream b)
65+
b
66+
(ByteArrayInputStream. b)))
67+
_ (.mark stream 512)
68+
iis (InflaterInputStream. stream)
69+
readable? (try (.read iis) true
70+
(catch ZipException _ false))]
71+
(.reset stream)
72+
(if readable?
73+
(InflaterInputStream. stream)
74+
(InflaterInputStream. stream (Inflater. true))))))
75+
76+
(defn deflate
77+
"Returns a deflate'd version of the given byte array."
78+
[b]
79+
(when b
80+
(IOUtils/toByteArray (DeflaterInputStream. (ByteArrayInputStream. b)))))
81+
82+
(comment "ZLib Example"
83+
(bytes->str (force-byte-array (inflate (deflate (str->bytes "test it!"))))))
84+
(comment "GZip Example"
85+
(bytes->str (gunzip (gzip (str->bytes "test it!")))))
86+
87+
(defn crc32
88+
^Long [^bytes b]
89+
(let [o (CRC32.)]
90+
(.update o b)
91+
(.getValue o)))
92+
93+
(defn crc64
94+
^Long [^bytes b]
95+
(let [o (CRC64.)]
96+
(.update o b)
97+
(.getValue o)))
98+
99+
(defn md5
100+
^bytes [^bytes b]
101+
(let [o (MessageDigest/getInstance "md5")]
102+
(.update o b)
103+
(.digest o)))
104+
105+
(defn sha-1
106+
^bytes [^bytes b]
107+
(let [o (MessageDigest/getInstance "sha1")]
108+
(.update o b)
109+
(.digest o)))
110+
111+
(defn sha-256
112+
^bytes [^bytes b]
113+
(let [o (MessageDigest/getInstance "sha256")]
114+
(.update o b)
115+
(.digest o)))

src-java/CRC64.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package zlib_tiny;
2+
3+
public class CRC64 {
4+
5+
private static final long poly = 0xC96C5795D7870F42L;
6+
private static final long[] crcTable = new long[256];
7+
8+
private long crc = -1;
9+
10+
static {
11+
for (int b = 0; b < crcTable.length; ++b) {
12+
long r = b;
13+
for (int i = 0; i < 8; ++i) {
14+
if ((r & 1) == 1)
15+
r = (r >>> 1) ^ poly;
16+
else
17+
r >>>= 1;
18+
}
19+
20+
crcTable[b] = r;
21+
}
22+
}
23+
24+
public CRC64() {
25+
}
26+
27+
public void update(byte b) {
28+
crc = crcTable[(b ^ (int) crc) & 0xFF] ^ (crc >>> 8);
29+
}
30+
31+
public void update(byte[] buf) {
32+
update(buf, 0, buf.length);
33+
}
34+
35+
public void update(byte[] buf, int off, int len) {
36+
int end = off + len;
37+
38+
while (off < end)
39+
crc = crcTable[(buf[off++] ^ (int) crc) & 0xFF] ^ (crc >>> 8);
40+
}
41+
42+
public long getValue() {
43+
return ~crc;
44+
}
45+
}

0 commit comments

Comments
 (0)