-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.mjs
More file actions
71 lines (59 loc) · 2.08 KB
/
Copy pathdecrypt.mjs
File metadata and controls
71 lines (59 loc) · 2.08 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
import {
AccessPolicyKms,
CoverCrypt,
KmsClient,
hexDecode,
hexEncode,
} from "cloudproof_js"
process.removeAllListeners("warning") // To remove experimental fetch warnings
;(async () => {
const useKms = process.argv.includes("--kms")
const userKeyBytesIndex = process.argv.indexOf("--userKeyBytesHexEncoded") + 1
const userKeyBytes = hexDecode(process.argv[userKeyBytesIndex])
const userKeyAccessPolicyIndex =
process.argv.indexOf("--userKeyAccessPolicy") + 1
const userKeyAccessPolicy = process.argv[userKeyAccessPolicyIndex]
const userKeyUIDIndex = process.argv.indexOf("--userKeyUID") + 1
let userKeyUID = process.argv[userKeyUIDIndex]
const encryptedDataHexEncodedIndex =
process.argv.indexOf("--encryptedDataHexEncoded") + 1
const encryptedData = hexDecode(process.argv[encryptedDataHexEncodedIndex])
let authenticationData
if (process.argv.includes("--authentication-data")) {
const authenticationDataIndex =
process.argv.indexOf("--authentication-data") + 1
authenticationData = new TextEncoder().encode(
process.argv[authenticationDataIndex],
)
}
let result
if (useKms) {
const client = new KmsClient(
`http://${process.env.KMS_HOST || "localhost"}:9998`,
process.env.AUTH0_TOKEN_1,
)
const userKeyAccessPolicyKms = new AccessPolicyKms(userKeyAccessPolicy)
if (!userKeyUID) {
const uniqueIdentifier = Math.random().toString(36).slice(2, 7)
userKeyUID = await client.importCoverCryptUserDecryptionKey(
uniqueIdentifier,
{ bytes: userKeyBytes, policy: userKeyAccessPolicyKms },
)
}
result = await client.coverCryptDecrypt(userKeyUID, encryptedData, {
authenticationData,
})
} else {
const { CoverCryptHybridDecryption } = await CoverCrypt()
const encryption = new CoverCryptHybridDecryption(userKeyBytes)
result = encryption.decrypt(encryptedData, {
authenticationData,
})
}
process.stdout.write(
JSON.stringify({
headerMetadata: hexEncode(result.headerMetadata),
plaintext: hexEncode(result.plaintext),
}),
)
})()