Debian Codegraph @

docs/crypto.md

raw
1 # Cryptographic and Security Functions
2
3 Sprig provides a couple of advanced cryptographic functions.
4
5 ## sha1sum
6
7 The `sha1sum` function receives a string, and computes it's SHA1 digest.
8
9 ```
10 sha1sum "Hello world!"
11 ```
12
13 ## sha256sum
14
15 The `sha256sum` function receives a string, and computes it's SHA256 digest.
16
17 ```
18 sha256sum "Hello world!"
19 ```
20
21 The above will compute the SHA 256 sum in an "ASCII armored" format that is
22 safe to print.
23
24 ## adler32sum
25
26 The `adler32sum` function receives a string, and computes its Adler-32 checksum.
27
28 ```
29 adler32sum "Hello world!"
30 ```
31 ## bcrypt
32
33 The `bcrypt` function receives a string, and generates its `bcrypt` hash.
34
35 ```
36 bcrypt "myPassword"
37 ```
38
39 ## htpasswd
40
41 The `htpasswd` function takes a `username` and `password` and generates a `bcrypt` hash of the password. The result can be used for basic authentication on an [Apache HTTP Server](https://httpd.apache.org/docs/2.4/misc/password_encryptions.html#basic).
42
43 ```
44 htpasswd "myUser" "myPassword"
45 ```
46
47 Note that it is insecure to store the password directly in the template.
48
49 ## randBytes
50
51 The `randBytes` function accepts a count `N` and generates a cryptographically
52 secure (uses ```crypto/rand```) random sequence of `N` bytes. The sequence is
53 returned as a base64 encoded string.
54
55 ```
56 randBytes 24
57 ```
58
59 ## derivePassword
60
61 The `derivePassword` function can be used to derive a specific password based on
62 some shared "master password" constraints. The algorithm for this is
63 [well specified](https://masterpassword.app/masterpassword-algorithm.pdf).
64
65 ```
66 derivePassword 1 "long" "password" "user" "example.com"
67 ```
68
69 Note that it is considered insecure to store the parts directly in the template.
70
71 ## genPrivateKey
72
73 The `genPrivateKey` function generates a new private key encoded into a PEM
74 block.
75
76 It takes one of the values for its first param:
77
78 - `ecdsa`: Generate an elliptic curve DSA key (P256)
79 - `dsa`: Generate a DSA key (L2048N256)
80 - `rsa`: Generate an RSA 4096 key
81 - `ed25519`: Generate an Ed25519 key
82
83 ## buildCustomCert
84
85 The `buildCustomCert` function allows customizing the certificate.
86
87 It takes the following string parameters:
88
89 - A base64 encoded PEM format certificate
90 - A base64 encoded PEM format private key
91
92 It returns a certificate object with the following attributes:
93
94 - `Cert`: A PEM-encoded certificate
95 - `Key`: A PEM-encoded private key
96
97 Example:
98
99 ```
100 $ca := buildCustomCert "base64-encoded-ca-crt" "base64-encoded-ca-key"
101 ```
102
103 Note that the returned object can be passed to the `genSignedCert` function
104 to sign a certificate using this CA.
105
106 ## genCA
107
108 The `genCA` function generates a new, self-signed x509 certificate authority using a
109 2048-bit RSA private key.
110
111 It takes the following parameters:
112
113 - Subject's common name (cn)
114 - Cert validity duration in days
115
116 It returns an object with the following attributes:
117
118 - `Cert`: A PEM-encoded certificate
119 - `Key`: A PEM-encoded private key
120
121 Example:
122
123 ```
124 $ca := genCA "foo-ca" 365
125 ```
126
127 Note that the returned object can be passed to the `genSignedCert` function
128 to sign a certificate using this CA.
129
130 ## genCAWithKey
131
132 The `genCAWithKey` function generates a new, self-signed x509 certificate authority using a
133 given private key.
134
135 It takes the following parameters:
136
137 - Subject's common name (cn)
138 - Cert validity duration in days
139 - Private key (PEM-encoded); DSA keys are not supported
140
141 It returns an object with the following attributes:
142
143 - `Cert`: A PEM-encoded certificate
144 - `Key`: A PEM-encoded private key
145
146 Example:
147
148 ```
149 $ca := genCAWithKey "foo-ca" 365 (genPrivateKey "rsa")
150 ```
151
152 Note that the returned object can be passed to the `genSignedCert` function
153 to sign a certificate using this CA.
154
155 ## genSelfSignedCert
156
157 The `genSelfSignedCert` function generates a new, self-signed x509 certificate using a
158 2048-bit RSA private key.
159
160 It takes the following parameters:
161
162 - Subject's common name (cn)
163 - Optional list of IPs; may be nil
164 - Optional list of alternate DNS names; may be nil
165 - Cert validity duration in days
166
167 It returns an object with the following attributes:
168
169 - `Cert`: A PEM-encoded certificate
170 - `Key`: A PEM-encoded private key
171
172 Example:
173
174 ```
175 $cert := genSelfSignedCert "foo.com" (list "10.0.0.1" "10.0.0.2") (list "bar.com" "bat.com") 365
176 ```
177
178 ## genSelfSignedCertWithKey
179
180 The `genSelfSignedCertWithKey` function generates a new, self-signed x509 certificate using a
181 given private key.
182
183 It takes the following parameters:
184
185 - Subject's common name (cn)
186 - Optional list of IPs; may be nil
187 - Optional list of alternate DNS names; may be nil
188 - Cert validity duration in days
189 - Private key (PEM-encoded); DSA keys are not supported
190
191 It returns an object with the following attributes:
192
193 - `Cert`: A PEM-encoded certificate
194 - `Key`: A PEM-encoded private key
195
196 Example:
197
198 ```
199 $cert := genSelfSignedCertWithKey "foo.com" (list "10.0.0.1" "10.0.0.2") (list "bar.com" "bat.com") 365 (genPrivateKey "ecdsa")
200 ```
201
202 ## genSignedCert
203
204 The `genSignedCert` function generates a new, x509 certificate signed by the
205 specified CA, using a 2048-bit RSA private key.
206
207 It takes the following parameters:
208
209 - Subject's common name (cn)
210 - Optional list of IPs; may be nil
211 - Optional list of alternate DNS names; may be nil
212 - Cert validity duration in days
213 - CA (see `genCA`)
214
215 Example:
216
217 ```
218 $ca := genCA "foo-ca" 365
219 $cert := genSignedCert "foo.com" (list "10.0.0.1" "10.0.0.2") (list "bar.com" "bat.com") 365 $ca
220 ```
221
222 ## genSignedCertWithKey
223
224 The `genSignedCertWithKey` function generates a new, x509 certificate signed by the
225 specified CA, using a given private key.
226
227 It takes the following parameters:
228
229 - Subject's common name (cn)
230 - Optional list of IPs; may be nil
231 - Optional list of alternate DNS names; may be nil
232 - Cert validity duration in days
233 - CA (see `genCA`)
234 - Private key (PEM-encoded); DSA keys are not supported
235
236 Example:
237
238 ```
239 $ca := genCA "foo-ca" 365
240 $cert := genSignedCert "foo.com" (list "10.0.0.1" "10.0.0.2") (list "bar.com" "bat.com") 365 $ca (genPrivateKey "ed25519")
241 ```
242
243 ## encryptAES
244
245 The `encryptAES` function encrypts text with AES-256 CBC and returns a base64 encoded string.
246
247 ```
248 encryptAES "secretkey" "plaintext"
249 ```
250
251 ## decryptAES
252
253 The `decryptAES` function receives a base64 string encoded by the AES-256 CBC
254 algorithm and returns the decoded text.
255
256 ```
257 "30tEfhuJSVRhpG97XCuWgz2okj7L8vQ1s6V9zVUPeDQ=" | decryptAES "secretkey"
258 ```