@casual-simulation/crypto 2.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils.js ADDED
@@ -0,0 +1,78 @@
1
+ import { fromByteArray, toByteArray } from 'base64-js';
2
+ /**
3
+ * The header that gets added to private keys formated as a PEM file.
4
+ */
5
+ export const PEM_PRIVATE_KEY_HEADER = '-----BEGIN PRIVATE KEY-----';
6
+ /**
7
+ * The footer that gets added to private keys formated as a PEM file.
8
+ */
9
+ export const PEM_PRIVATE_KEY_FOOTER = '-----END PRIVATE KEY-----';
10
+ /**
11
+ * The header that gets added to public keys formated as a PEM file.
12
+ */
13
+ export const PEM_PUBLIC_KEY_HEADER = '-----BEGIN PUBLIC KEY-----';
14
+ /**
15
+ * The footer that gets added to public keys formated as a PEM file.
16
+ */
17
+ export const PEM_PUBLIC_KEY_FOOTER = '-----END PUBLIC KEY-----';
18
+ /**
19
+ * Formats the given buffer as a PEM file for public keys.
20
+ * @param key The buffer that should be stored in the PEM file.
21
+ */
22
+ export function formatPublicPEMKey(key) {
23
+ return formatPEM(key, PEM_PUBLIC_KEY_HEADER, PEM_PUBLIC_KEY_FOOTER);
24
+ }
25
+ /**
26
+ * Formats the given buffer as a PEM file for private keys.
27
+ * @param key The buffer that should be stored in the PEM file.
28
+ */
29
+ export function formatPrivatePEMKey(key) {
30
+ return formatPEM(key, PEM_PRIVATE_KEY_HEADER, PEM_PRIVATE_KEY_FOOTER);
31
+ }
32
+ /**
33
+ * Formats the given buffer into PEM format.
34
+ * @param buffer The buffer to format.
35
+ * @param header The header to use.
36
+ * @param footer The footer to use.
37
+ */
38
+ export function formatPEM(buffer, header, footer) {
39
+ const bytes = new Uint8Array(buffer);
40
+ const base64 = fromByteArray(bytes);
41
+ const pem = `${header}\n${base64}\n${footer}`;
42
+ return pem;
43
+ }
44
+ /**
45
+ * Parses the given private key PEM file into a buffer that contains just the key.
46
+ * @param pem The PEM file that represents the private key.
47
+ */
48
+ export function parsePrivatePEMKey(pem) {
49
+ return parsePEM(pem, PEM_PRIVATE_KEY_HEADER, PEM_PRIVATE_KEY_FOOTER);
50
+ }
51
+ /**
52
+ * Parses the given public key PEM file into a buffer that contains just the key.
53
+ * @param pem The PEM file that represents the public key.
54
+ */
55
+ export function parsePublicPEMKey(pem) {
56
+ return parsePEM(pem, PEM_PUBLIC_KEY_HEADER, PEM_PUBLIC_KEY_FOOTER);
57
+ }
58
+ /**
59
+ * Parses the given PEM file using the given header and footer strings.
60
+ * Returns an ArrayBuffer containing the bytes that were formatted into the PEM file.
61
+ *
62
+ * Note that this should probably not be used for PEM files other than the ones produced by
63
+ * formatPEM(). This is because PEM files can contain a lot of extra data that this implementation
64
+ * does not expect. For example, some PEM files can contain multiple keys and probably allow more whitespace.
65
+ *
66
+ * @param pem The PEM file to parse.
67
+ * @param header The header that we're expecting the file to have.
68
+ * @param footer The footer that we're expecting the file to have.
69
+ */
70
+ export function parsePEM(pem, header, footer) {
71
+ pem = pem.trim();
72
+ // Make sure we remove the newlines
73
+ let contents = pem.substring(header.length + 1, pem.length - (footer.length + 1));
74
+ contents = contents.replace('\n', '');
75
+ const buffer = toByteArray(contents);
76
+ return buffer.buffer;
77
+ }
78
+ //# sourceMappingURL=utils.js.map
package/utils.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAEvD;;GAEG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAW,6BAA6B,CAAC;AAE5E;;GAEG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAW,2BAA2B,CAAC;AAE1E;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAW,4BAA4B,CAAC;AAE1E;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAW,0BAA0B,CAAC;AAExE;;;GAGG;AACH,MAAM,UAA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