2019-11-20 13:23:36 +01:00
|
|
|
import { Duplex } from "it-pair";
|
2019-11-12 14:02:59 +01:00
|
|
|
import { NoiseSession } from "./xx";
|
2019-11-25 13:09:40 +01:00
|
|
|
import { Handshake } from "./handshake";
|
2019-11-12 14:02:59 +01:00
|
|
|
|
2019-11-25 13:09:40 +01:00
|
|
|
interface IReturnEncryptionWrapper {
|
|
|
|
(source: any): any;
|
2019-11-12 14:02:59 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 13:09:40 +01:00
|
|
|
// Returns generator that encrypts payload from the user
|
|
|
|
export function encryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper {
|
|
|
|
return async function * (source) {
|
|
|
|
for await (const chunk of source) {
|
|
|
|
const data = await handshake.encrypt(chunk, session);
|
|
|
|
yield data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 14:02:59 +01:00
|
|
|
|
|
|
|
|
2019-11-25 13:09:40 +01:00
|
|
|
// Decrypt received payload to the user
|
2019-11-25 13:27:55 +01:00
|
|
|
export function decryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper {
|
2019-11-25 13:09:40 +01:00
|
|
|
return async function * (source) {
|
|
|
|
for await (const chunk of source) {
|
|
|
|
const decrypted = await handshake.decrypt(chunk, session);
|
|
|
|
yield decrypted
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 14:02:59 +01:00
|
|
|
}
|