decrypt.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const crypto = require("crypto");
  2. function decryptString(dataKey, cipherText) {
  3. const buffer = decrypt(dataKey, cipherText);
  4. if (buffer === null) {
  5. return null;
  6. }
  7. const str = buffer.toString('utf-8');
  8. if (str === 'false') {
  9. throw new Error("Could not decrypt string.");
  10. }
  11. return str;
  12. }
  13. function decrypt(key, cipherText, ivLength = 13) {
  14. if (cipherText === null) {
  15. return null;
  16. }
  17. if (!key) {
  18. return "[protected]";
  19. }
  20. try {
  21. const cipherTextBufferWithIv = Buffer.from(cipherText.toString(), 'base64');
  22. const iv = cipherTextBufferWithIv.slice(0, ivLength);
  23. const cipherTextBuffer = cipherTextBufferWithIv.slice(ivLength);
  24. const decipher = crypto.createDecipheriv('aes-128-cbc', pad(key), pad(iv));
  25. const decryptedBytes = Buffer.concat([decipher.update(cipherTextBuffer), decipher.final()]);
  26. const digest = decryptedBytes.slice(0, 4);
  27. const payload = decryptedBytes.slice(4);
  28. const computedDigest = shaArray(payload).slice(0, 4);
  29. if (!arraysIdentical(digest, computedDigest)) {
  30. return false;
  31. }
  32. return payload;
  33. }
  34. catch (e) {
  35. // recovery from https://github.com/zadam/trilium/issues/510
  36. if (e.message?.includes("WRONG_FINAL_BLOCK_LENGTH") || e.message?.includes("wrong final block length")) {
  37. log.info("Caught WRONG_FINAL_BLOCK_LENGTH, returning cipherText instead");
  38. return cipherText;
  39. }
  40. else {
  41. throw e;
  42. }
  43. }
  44. }
  45. function pad(data) {
  46. if (data.length > 16) {
  47. data = data.slice(0, 16);
  48. }
  49. else if (data.length < 16) {
  50. const zeros = Array(16 - data.length).fill(0);
  51. data = Buffer.concat([data, Buffer.from(zeros)]);
  52. }
  53. return Buffer.from(data);
  54. }
  55. function arraysIdentical(a, b) {
  56. let i = a.length;
  57. if (i !== b.length) return false;
  58. while (i--) {
  59. if (a[i] !== b[i]) return false;
  60. }
  61. return true;
  62. }
  63. function shaArray(content) {
  64. // we use this as simple checksum and don't rely on its security so SHA-1 is good enough
  65. return crypto.createHash('sha1').update(content).digest();
  66. }
  67. module.exports = {
  68. decrypt,
  69. decryptString
  70. };