forked from suvash/one-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrgen.clj
74 lines (69 loc) · 2.74 KB
/
qrgen.clj
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
(ns one-time.qrgen
(:require [one-time.uri :as uri])
(:import [java.io ByteArrayOutputStream]
[javax.imageio ImageIO]
[io.nayuki.qrcodegen QrCode QrCode$Ecc]
))
(def ^:private image-types
{:JPG "JPG"
:GIF "GIF"
:PNG "PNG"
:BMP "BMP"})
(defn totp-stream
"Returns a java.io.ByteArrayOutputStream with the totp qrcode"
[{:keys [image-type image-size label user secret]
:or {image-type :JPG image-size 3}}]
{:pre [(not-any? nil? [label user secret])
(image-types image-type)]}
(let [baos (ByteArrayOutputStream.)]
(-> (^String uri/totp-uri {:label label
:secret secret
:user user})
(QrCode/encodeText QrCode$Ecc/HIGH)
(.toImage image-size 0)
(ImageIO/write ^String (image-types image-type) baos))
baos))
(defn totp-file
"Returns a java.io.File with the totp qrcode"
[{:keys [image-type image-size label user secret]
:or {image-type :JPG image-size 3}}]
{:pre [(not-any? nil? [label user secret])
(image-types image-type)]}
(let [f (java.io.File/createTempFile "qrgen" (str "." (image-types image-type)))]
(-> (^String uri/totp-uri {:label label
:secret secret
:user user})
(QrCode/encodeText QrCode$Ecc/HIGH)
(.toImage image-size 0)
(ImageIO/write ^String (image-types image-type) f))
f))
(defn hotp-stream
"Returns a java.io.ByteArrayOutputStream with the hotp qrcode"
[{:keys [image-type image-size label user secret counter]
:or {image-type :JPG image-size 3}}]
{:pre [(not-any? nil? [label user secret counter])
(image-types image-type)]}
(let [baos (ByteArrayOutputStream.)]
(-> (^String uri/hotp-uri {:label label
:secret secret
:user user
:counter counter})
(QrCode/encodeText QrCode$Ecc/HIGH)
(.toImage image-size 0)
(ImageIO/write ^String (image-types image-type) baos))
baos))
(defn hotp-file
"Returns a java.io.File with the hotp qrcode"
[{:keys [image-type image-size label user secret counter]
:or {image-type :JPG image-size 3}}]
{:pre [(not-any? nil? [label user secret counter])
(image-types image-type)]}
(let [f (java.io.File/createTempFile "qrgen" (str "." (image-types image-type)))]
(-> (^String uri/hotp-uri {:label label
:secret secret
:user user
:counter counter})
(QrCode/encodeText QrCode$Ecc/HIGH)
(.toImage image-size 0)
(ImageIO/write ^String (image-types image-type) f))
f))