code cleanup

This commit is contained in:
Adhiraj Singh
2021-10-11 09:36:17 +05:30
parent c75364ada8
commit d3b7ffecf2
10 changed files with 26 additions and 22 deletions

View File

@@ -2,7 +2,7 @@ import { Boom } from '@hapi/boom'
import { aesDecrypt, hmacSign, aesEncrypt, hkdf } from "./crypto"
import { AuthenticationState, WAPatchCreate, ChatMutation, WAPatchName, LTHashState, ChatModification } from "../Types"
import { proto } from '../../WAProto'
import { LT_HASH_ANTI_TAMPERING } from '../WABinary/LTHash'
import { LT_HASH_ANTI_TAMPERING } from './lt-hash'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren } from '../WABinary'
import { toNumber } from './generics'

View File

@@ -3,4 +3,9 @@ export * from './generics'
export * from './messages'
export * from './messages-media'
export * from './validate-connection'
export * from './crypto'
export * from './crypto'
export * from './signal'
export * from './noise-handler'
export * from './history'
export * from './chat-utils'
export * from './lt-hash'

57
src/Utils/lt-hash.ts Normal file
View File

@@ -0,0 +1,57 @@
import { hkdf } from './crypto'
/**
* LT Hash is a summation based hash algorithm that maintains the integrity of a piece of data
* over a series of mutations. You can add/remove mutations and it'll return a hash equal to
* if the same series of mutations was made sequentially.
*/
const o = 128;
class d {
salt: string
constructor(e: string) {
this.salt = e
}
add(e, t) {
var r = this;
for(const item of t) {
e = r._addSingle(e, item)
}
return e
}
subtract(e, t) {
var r = this;
for(const item of t) {
e = r._subtractSingle(e, item)
}
return e
}
subtractThenAdd(e, t, r) {
var n = this;
return n.add(n.subtract(e, r), t)
}
_addSingle(e, t) {
var r = this;
const n = new Uint8Array(hkdf(Buffer.from(t), o, { info: r.salt })).buffer;
return r.performPointwiseWithOverflow(e, n, ((e,t)=>e + t))
}
_subtractSingle(e, t) {
var r = this;
const n = new Uint8Array(hkdf(Buffer.from(t), o, { info: r.salt })).buffer;
return r.performPointwiseWithOverflow(e, n, ((e,t)=>e - t))
}
performPointwiseWithOverflow(e, t, r) {
const n = new DataView(e)
, i = new DataView(t)
, a = new ArrayBuffer(n.byteLength)
, s = new DataView(a);
for (let e = 0; e < n.byteLength; e += 2)
s.setUint16(e, r(n.getUint16(e, !0), i.getUint16(e, !0)), !0);
return a
}
}
export const LT_HASH_ANTI_TAMPERING = new d('WhatsApp Patch Integrity')

View File

@@ -14,7 +14,7 @@ const generateIV = (counter: number) => {
return new Uint8Array(iv)
}
export default ({ public: publicKey, private: privateKey }: KeyPair) => {
export const makeNoiseHandler = ({ public: publicKey, private: privateKey }: KeyPair) => {
const authenticate = (data: Uint8Array) => {
if(!isFinished) {