Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x | import { createCipheriv, createDecipheriv, pbkdf2Sync } from 'crypto';
import { deriveStringToBuffer } from '../utils/stringCoding';
import { IEncryptionAlgorithm, EncryptionResult, IEncryptionAlgorithmConfig } from './IEncryptionAlgorithm';
import { encodeText, decodeText, TextEncoding } from '../utils/encodingUtils';
const DEFAULT_TAG_LENGTH = 16;
const DEFAULT_KEY_LENGTH = 32;
const DEFAULT_ITERATIONS = 100000;
const DEFAULT_NONCE_LENGTH = 12;
const DEFAULT_SALT = deriveStringToBuffer('easy-cipher-mate', 16);
const DEFAULT_NONCE = deriveStringToBuffer('easy-cipher-mate', DEFAULT_NONCE_LENGTH);
export interface IChaCha20Poly1305EncryptionConfig extends IEncryptionAlgorithmConfig {
password: string;
salt: Buffer;
nonce: Buffer;
textEncoding?: TextEncoding;
}
export class ChaCha20Poly1305Encryption implements IEncryptionAlgorithm<IChaCha20Poly1305EncryptionConfig> {
public static TAG_LENGTH = DEFAULT_TAG_LENGTH;
public static KEY_LENGTH = DEFAULT_KEY_LENGTH;
public static ITERATIONS = DEFAULT_ITERATIONS;
public static NONCE_LENGTH = DEFAULT_NONCE_LENGTH;
async encryptText(plaintext: string, config: IChaCha20Poly1305EncryptionConfig): Promise<EncryptionResult> {
const { password, salt, nonce, textEncoding = 'utf-8' } = config;
this.validateNonce(nonce);
const key = this.deriveKey(password, salt);
const textBuffer = encodeText(plaintext, textEncoding);
const cipher = createCipheriv('chacha20-poly1305', key, nonce, {
authTagLength: ChaCha20Poly1305Encryption.TAG_LENGTH,
});
const encrypted = Buffer.concat([
cipher.update(Buffer.from(textBuffer)),
cipher.final()
]);
const tag = cipher.getAuthTag();
console.log('Encrypted length:', encrypted.length);
console.log('Tag length:', tag.length);
console.log('Buffer.concat([encrypted, tag]) length:', Buffer.concat([encrypted, tag]).length);
return { data: Buffer.concat([encrypted, tag]).buffer };
}
async decryptText(encryptedData: ArrayBuffer, config: IChaCha20Poly1305EncryptionConfig): Promise<string> {
const { password, salt, nonce, textEncoding = 'utf-8' } = config;
this.validateNonce(nonce);
const key = this.deriveKey(password, salt);
const data = Buffer.from(encryptedData);
console.log('Data length:', data.length);
console.log('Tag length:', ChaCha20Poly1305Encryption.TAG_LENGTH);
console.log('Data length - Tag length:', data.length - ChaCha20Poly1305Encryption.TAG_LENGTH);
const tag = data.subarray(data.length - ChaCha20Poly1305Encryption.TAG_LENGTH);
const ciphertext = data.subarray(0, data.length - ChaCha20Poly1305Encryption.TAG_LENGTH);
const decipher = createDecipheriv('chacha20-poly1305', key, nonce, {
authTagLength: ChaCha20Poly1305Encryption.TAG_LENGTH,
})
decipher.setAuthTag(tag);
const decrypted = Buffer.concat([
decipher.update(ciphertext),
decipher.final()
]);
return decodeText(decrypted.buffer, textEncoding);
}
async encryptFile(fileBuffer: ArrayBuffer, config: IChaCha20Poly1305EncryptionConfig): Promise<EncryptionResult> {
const { password, salt, nonce } = config;
this.validateNonce(nonce);
const key = this.deriveKey(password, salt);
const dataBuffer = Buffer.from(fileBuffer);
const cipher = createCipheriv('chacha20-poly1305', key, nonce, {
authTagLength: ChaCha20Poly1305Encryption.TAG_LENGTH,
})
const encrypted = Buffer.concat([
cipher.update(dataBuffer),
cipher.final()
]);
const tag = cipher.getAuthTag();
return { data: Buffer.concat([tag, encrypted]).buffer };
}
async decryptFile(encryptedBuffer: ArrayBuffer, config: IChaCha20Poly1305EncryptionConfig): Promise<ArrayBuffer> {
const { password, salt, nonce } = config;
this.validateNonce(nonce);
const key = this.deriveKey(password, salt);
const data = Buffer.from(encryptedBuffer);
const tag = data.subarray(0, ChaCha20Poly1305Encryption.TAG_LENGTH);
const ciphertext = data.subarray(ChaCha20Poly1305Encryption.TAG_LENGTH);
const decipher = createDecipheriv('chacha20-poly1305', key, nonce, {
authTagLength: ChaCha20Poly1305Encryption.TAG_LENGTH,
})
decipher.setAuthTag(tag);
const decrypted = Buffer.concat([
decipher.update(ciphertext),
decipher.final()
]);
return decrypted.buffer;
}
private deriveKey(password: string, salt: Buffer): Buffer {
return pbkdf2Sync(
password,
salt,
ChaCha20Poly1305Encryption.ITERATIONS,
ChaCha20Poly1305Encryption.KEY_LENGTH,
'sha256'
);
}
public validateNonce(nonce: Buffer): void {
Iif (nonce.length !== ChaCha20Poly1305Encryption.NONCE_LENGTH) {
throw new Error(`Nonce must be ${ChaCha20Poly1305Encryption.NONCE_LENGTH} bytes`)
}
}
}
export class ChaCha20Poly1305EncryptionConfigFromEnv implements IChaCha20Poly1305EncryptionConfig {
password: string;
salt: Buffer;
nonce: Buffer;
textEncoding?: TextEncoding;
constructor(
password?: string,
salt?: Buffer,
nonce?: Buffer,
textEncoding?: TextEncoding
) {
this.password = password ?? process.env.ECM_CHACHA20_PASSWORD ?? '';
this.salt = salt ?? (process.env.ECM_CHACHA20_SALT
? deriveStringToBuffer(process.env.ECM_CHACHA20_SALT, 16)
: DEFAULT_SALT);
if (nonce) {
this.nonce = nonce;
} else if (process.env.ECM_CHACHA20_NONCE) {
this.nonce = deriveStringToBuffer(
process.env.ECM_CHACHA20_NONCE,
ChaCha20Poly1305Encryption.NONCE_LENGTH
);
} else {
this.nonce = DEFAULT_NONCE;
}
this.textEncoding = textEncoding
?? (process.env.ECM_TEXT_ENCODING as TextEncoding)
?? 'utf-8';
Iif (this.nonce.length !== ChaCha20Poly1305Encryption.NONCE_LENGTH) {
throw new Error(`Length of nonce must be ${ChaCha20Poly1305Encryption.NONCE_LENGTH} bytes`)
}
}
}
|