Skip to content

Commit

Permalink
code fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongFuze committed Jan 6, 2023
1 parent 9669637 commit 994d245
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 61 deletions.
19 changes: 11 additions & 8 deletions src/__tests__/Greeter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import DARE from '../index';
import * as path from 'path';
import * as fs from 'fs';
jest.setTimeout(10000)
jest.setTimeout(10000);

function readFile(filename: string): Buffer {
const content = fs.readFileSync(filename);
Expand All @@ -27,7 +27,7 @@ it('encrypt derive key: Buffer', async () => {
const dare = new DARE(password);
const [deriveKey2, salt] = await Promise.resolve(dare.EncryptDeriveKey());
console.log(deriveKey2, salt);
writeFile(dst, salt)
writeFile(dst, salt);
// use deriveKey ...
});

Expand All @@ -44,12 +44,15 @@ it('decrypt derive key: Buffer', async () => {
const src = readFile(path.join(process.cwd(), 'README.md.enc')); // Buffer
const password = '012345678901234567890';
const dare = new DARE(password);
await dare.DecryptDeriveKey(src).then(deriveKey => {
// use deriveKey ...
console.log(deriveKey);
}).catch(err => {
console.error(err.message);
});
await dare
.DecryptDeriveKey(src)
.then((deriveKey) => {
// use deriveKey ...
console.log(deriveKey);
})
.catch((err) => {
console.error(err.message);
});
});

it('Encrypt: path -> path', async () => {
Expand Down
63 changes: 27 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export default class DARE {
}
EncryptDeriveKey(dst: fs.PathLike): Promise<Uint8Array>;
EncryptDeriveKey(dst?: undefined): Promise<[Uint8Array, Buffer]>;
EncryptDeriveKey(dst?: Buffer | fs.PathLike | undefined):
Promise<Uint8Array | [Uint8Array, Buffer]> | void {
EncryptDeriveKey(dst?: Buffer | fs.PathLike | undefined): Promise<Uint8Array | [Uint8Array, Buffer]> | void {
const salt = crypto.randomBytes(32);
const N = 32768;
const r = 16;
Expand Down Expand Up @@ -51,9 +50,7 @@ export default class DARE {
});
}
}
DecryptDeriveKey(src: fs.PathLike): Promise<Uint8Array>;
DecryptDeriveKey(src: Buffer): Promise<Uint8Array>;
DecryptDeriveKey(src: Buffer | fs.PathLike): Promise<Uint8Array> | void {
DecryptDeriveKey(src: Buffer | fs.PathLike): Promise<Uint8Array> {
const N = 32768;
const r = 16;
const p = 1;
Expand Down Expand Up @@ -81,26 +78,23 @@ export default class DARE {
if (src.byteLength < 32) {
throw Error('failed to read salt from src');
}
const salt = src.subarray(0, 32)
const salt = src.subarray(0, 32);
const key = scrypt.syncScrypt(this.password, salt, N, r, p, dkLen);
resolve(key);
} else {
throw Error('src must be a "Buffer" object');
}
} catch (err: any) {
reject(err)
reject(err);
}
});
}
}
Encrypt(src: fs.PathLike, dst: fs.PathLike): void;
Encrypt(src: fs.PathLike, dst?: undefined): Promise<Buffer>;
Encrypt(src: Buffer, dst: fs.PathLike): void;
Encrypt(src: Buffer, dst?: undefined): Promise<Buffer>;
async Encrypt(src: Buffer | fs.PathLike, dst?: Buffer | fs.PathLike | undefined):
Promise<Buffer | number | void> {
Encrypt(src: fs.PathLike | Buffer, dst: fs.PathLike): void;
Encrypt(src: fs.PathLike | Buffer, dst?: undefined): Promise<Buffer>;
async Encrypt(src: Buffer | fs.PathLike, dst?: Buffer | fs.PathLike | undefined): Promise<Buffer | number | void> {
if (dst != null && isAbsolute(dst!.toString())) {
const deriveKey = (await Promise.resolve(this.EncryptDeriveKey(dst)));
const deriveKey = await Promise.resolve(this.EncryptDeriveKey(dst));
const writeStream = fs.createWriteStream(dst, { flags: 'a' });
if (isAbsolute(src.toString())) {
// read stream -> write stream
Expand Down Expand Up @@ -132,8 +126,8 @@ export default class DARE {
if (Buffer.isBuffer(src)) {
const count = Math.ceil(src.byteLength / _const.MaxPayloadSize);
for (let i = 0; i < count; i++) {
let s = i * _const.MaxPayloadSize;
let e = Math.min(src.byteLength, (i+1) * _const.MaxPayloadSize);
const s = i * _const.MaxPayloadSize;
const e = Math.min(src.byteLength, (i + 1) * _const.MaxPayloadSize);
const chunk = src.subarray(s, e);
const cipherText = reader.Read(Buffer.from(chunk));
writeStream.write(cipherText);
Expand All @@ -153,9 +147,9 @@ export default class DARE {
}
} else {
// dst is null
const [deriveKey, salt] = (await Promise.resolve(this.EncryptDeriveKey()));
const [deriveKey, salt] = await Promise.resolve(this.EncryptDeriveKey());
const chunks: Buffer[] = [];
chunks.push(salt)
chunks.push(salt);
if (isAbsolute(src.toString())) {
// read stream -> Buffer
const readStream = fs.createReadStream(src, { highWaterMark: _const.MaxPayloadSize, start: 0 });
Expand Down Expand Up @@ -185,8 +179,8 @@ export default class DARE {
if (Buffer.isBuffer(src)) {
const count = Math.ceil(src.byteLength / _const.MaxPayloadSize);
for (let i = 0; i < count; i++) {
let s = i * _const.MaxPayloadSize;
let e = Math.min(src.byteLength, (i+1) * _const.MaxPayloadSize);
const s = i * _const.MaxPayloadSize;
const e = Math.min(src.byteLength, (i + 1) * _const.MaxPayloadSize);
const chunk = src.subarray(s, e);
const cipherText = reader.Read(Buffer.from(chunk));
chunks.push(cipherText);
Expand All @@ -205,14 +199,11 @@ export default class DARE {
}
}
}
Decrypt(src: fs.PathLike, dst: fs.PathLike): void;
Decrypt(src: fs.PathLike, dst?: undefined): Promise<Buffer>;
Decrypt(src: Buffer, dst: fs.PathLike): void;
Decrypt(src: Buffer, dst?: undefined): Promise<Buffer>;
async Decrypt(src: Buffer | fs.PathLike, dst?: Buffer | fs.PathLike | undefined):
Promise<Buffer | number | void> {
Decrypt(src: fs.PathLike | Buffer, dst: fs.PathLike): void;
Decrypt(src: fs.PathLike | Buffer, dst?: undefined): Promise<Buffer>;
async Decrypt(src: Buffer | fs.PathLike, dst?: Buffer | fs.PathLike | undefined): Promise<Buffer | number | void> {
if (dst != null && isAbsolute(dst!.toString())) {
const deriveKey = (await Promise.resolve(this.DecryptDeriveKey(src))); // as Uint8Array;
const deriveKey = await Promise.resolve(this.DecryptDeriveKey(src)); // as Uint8Array;
const writeStream = fs.createWriteStream(dst);
if (isAbsolute(src.toString())) {
// read stream -> write stream
Expand All @@ -236,17 +227,17 @@ export default class DARE {
} else {
// Buffer block -> write stream
let n = 0;
const salt_offset = 32
const saltOffset = 32;
const config = new Config(deriveKey);
config.setConfigDefaults();
const reader = new DecReader(config);
return new Promise((resolve, reject) => {
try {
if (Buffer.isBuffer(src)) {
const count = Math.ceil((src.byteLength - salt_offset) / _const.MaxPackageSize);
const count = Math.ceil((src.byteLength - saltOffset) / _const.MaxPackageSize);
for (let i = 0; i < count; i++) {
let s = i * _const.MaxPackageSize + salt_offset;
let e = Math.min(src.byteLength, (i+1) * _const.MaxPackageSize + salt_offset);
const s = i * _const.MaxPackageSize + saltOffset;
const e = Math.min(src.byteLength, (i + 1) * _const.MaxPackageSize + saltOffset);
const chunk = src.subarray(s, e);
const plainText = reader.Read(Buffer.from(chunk));
writeStream.write(plainText);
Expand All @@ -266,7 +257,7 @@ export default class DARE {
}
} else {
// dst is null
const deriveKey = (await Promise.resolve(this.DecryptDeriveKey(src))); // as Uint8Array;
const deriveKey = await Promise.resolve(this.DecryptDeriveKey(src)); // as Uint8Array;
const chunks: Buffer[] = [];
if (isAbsolute(src.toString())) {
// read stream -> Buffer
Expand All @@ -289,17 +280,17 @@ export default class DARE {
} else {
// Buffer -> Buffer
let n = 0;
const salt_offset = 32
const saltOffset = 32;
const config = new Config(deriveKey);
config.setConfigDefaults();
const reader = new DecReader(config);
return new Promise((resolve, reject) => {
try {
if (Buffer.isBuffer(src)) {
const count = Math.ceil((src.byteLength - salt_offset) / _const.MaxPackageSize);
const count = Math.ceil((src.byteLength - saltOffset) / _const.MaxPackageSize);
for (let i = 0; i < count; i++) {
let s = i * _const.MaxPackageSize + salt_offset;
let e = Math.min(src.byteLength, (i+1) * _const.MaxPackageSize + salt_offset);
const s = i * _const.MaxPackageSize + saltOffset;
const e = Math.min(src.byteLength, (i + 1) * _const.MaxPackageSize + saltOffset);
const chunk = src.subarray(s, e);
const plainText = reader.Read(Buffer.from(chunk));
chunks.push(plainText);
Expand Down
33 changes: 16 additions & 17 deletions src/pathutils.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
function unix_absolute(path: string) {
const re = /^\/[^\\/](?!\.{1,2}($|\/))((?!\/[./](\/|$))\/?.)*\/?(?<!\/\.{1,1})$/i
const re = /^\/[^\\/](?!\.{1,2}($|\/))((?!\/[./](\/|$))\/?.)*\/?(?<!\/\.{1,1})$/i;

return re.test(path)
return re.test(path);
}

function unix_relative(path: string) {
const re = /^.{1,2}\/(?:.{2}|\w)*$/i
return re.test(path)
const re = /^.{1,2}\/(?:.{2}|\w)*$/i;
return re.test(path);
}

function win_absolute(path: string) {
const re = /^(?:(?:[a-z]:\\)|\\)(((?![<>:"/\\|?*]).)+(?:(?<![ .])\\)?)*$/i
return re.test(path)
const re = /^(?:(?:[a-z]:\\)|\\)(((?![<>:"/\\|?*]).)+(?:(?<![ .])\\)?)*$/i;
return re.test(path);
}

function unix_valid(path: string) {
return unix_absolute(path) || unix_relative(path)
return unix_absolute(path) || unix_relative(path);
}

function win_relative(path: string) {
const re =
/^(?:(?:[a-z]:)|[a-z0-9]|\.{2}\\)(?:(?:(?:(?![<>:"/\\|?*]).)|(?:\.{2}))+(?:(?<![ .]{3})\\)?)*$/i
return re.test(path)
const re = /^(?:(?:[a-z]:)|[a-z0-9]|\.{2}\\)(?:(?:(?:(?![<>:"/\\|?*]).)|(?:\.{2}))+(?:(?<![ .]{3})\\)?)*$/i;
return re.test(path);
}

function win_valid(path: string) {
return win_absolute(path) || win_relative(path)
return win_absolute(path) || win_relative(path);
}

export function isPath(path: string) {
return win_valid(path) || unix_valid(path)
return win_valid(path) || unix_valid(path);
}

export function isAbsolute(path: string) {
return win_absolute(path) || unix_absolute(path)
return win_absolute(path) || unix_absolute(path);
}

export function relative(path: string) {
return win_relative(path) || unix_relative(path)
return win_relative(path) || unix_relative(path);
}

export function absolute(path: string) {
return win_absolute(path) || unix_absolute(path)
return win_absolute(path) || unix_absolute(path);
}

export function win(path: string) {
return win_valid(path)
return win_valid(path);
}

export function unix(path: string) {
return unix_valid(path)
return unix_valid(path);
}

0 comments on commit 994d245

Please sign in to comment.