File size: 2,352 Bytes
46463e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
/**
 * @license
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * Encrypts a plain text string utilizing a rotation and salt key
 * to prevent exposure of sensitive transaction / invoice details.
 */
export function encryptData(plainText: string, secretKey: string): string {
  const salt = "HBT-SECURE-VAULT-" + secretKey.length;
  const combined = salt + "||" + plainText;
  
  // Convert secretKey into a simple numeric pad
  let pad = 0;
  for (let i = 0; i < secretKey.length; i++) {
    pad += secretKey.charCodeAt(i);
  }
  
  // Rotate characters for obfuscation
  const chars: string[] = [];
  for (let i = 0; i < combined.length; i++) {
    let charCode = combined.charCodeAt(i);
    // Apply rotational key padding
    charCode = (charCode + pad + i) % 65535;
    chars.push(String.fromCharCode(charCode));
  }
  
  const scrambled = chars.join('');
  // Encode as Base64 safely under browser context
  try {
    return btoa(unescape(encodeURIComponent(scrambled)));
  } catch (e) {
    // Fallback if there are broad unicode failures
    return btoa(scrambled);
  }
}

/**
 * Decrypts a scrambled cipher string back to original plain text
 * using the validated passkey. Throws error on fail.
 */
export function decryptData(cipherText: string, secretKey: string): string {
  let scrambled = '';
  try {
    scrambled = decodeURIComponent(escape(atob(cipherText)));
  } catch (e) {
    try {
      scrambled = atob(cipherText);
    } catch (err) {
      throw new Error("Invalid file structure: Decryption failed.");
    }
  }

  let pad = 0;
  for (let i = 0; i < secretKey.length; i++) {
    pad += secretKey.charCodeAt(i);
  }

  const chars: string[] = [];
  for (let i = 0; i < scrambled.length; i++) {
    let charCode = scrambled.charCodeAt(i);
    // Reverse rotation
    charCode = (charCode - pad - i) % 65535;
    while (charCode < 0) charCode += 65535;
    chars.push(String.fromCharCode(charCode));
  }

  const decrypted = chars.join('');
  // Validate presence of our prefix
  if (!decrypted.includes("||")) {
    throw new Error("Invalid password: Secure decryption key mismatch.");
  }

  const parts = decrypted.split("||");
  const salt = parts[0];
  const payload = parts.slice(1).join("||");

  if (!salt.startsWith("HBT-SECURE-VAULT-")) {
    throw new Error("Corrupted backup data block.");
  }

  return payload;
}