all: format with prettier
(cherry picked from commit cad5b4d7deba4fbe4a40a17306ce49d3b2f13139)
This commit is contained in:
100
src/utils/tea.ts
100
src/utils/tea.ts
@@ -15,68 +15,66 @@
|
||||
// where compatibility with legacy systems, not security, is the goal.
|
||||
|
||||
export class TeaCipher {
|
||||
// BlockSize is the size of a TEA block, in bytes.
|
||||
static readonly BlockSize = 8;
|
||||
// BlockSize is the size of a TEA block, in bytes.
|
||||
static readonly BlockSize = 8;
|
||||
|
||||
// KeySize is the size of a TEA key, in bytes.
|
||||
static readonly KeySize = 16;
|
||||
// KeySize is the size of a TEA key, in bytes.
|
||||
static readonly KeySize = 16;
|
||||
|
||||
// delta is the TEA key schedule constant.
|
||||
static readonly delta = 0x9e3779b9;
|
||||
// delta is the TEA key schedule constant.
|
||||
static readonly delta = 0x9e3779b9;
|
||||
|
||||
// numRounds 64 is the standard number of rounds in TEA.
|
||||
static readonly numRounds = 64;
|
||||
// numRounds 64 is the standard number of rounds in TEA.
|
||||
static readonly numRounds = 64;
|
||||
|
||||
k0: number
|
||||
k1: number
|
||||
k2: number
|
||||
k3: number
|
||||
rounds: number
|
||||
k0: number;
|
||||
k1: number;
|
||||
k2: number;
|
||||
k3: number;
|
||||
rounds: number;
|
||||
|
||||
constructor(key: Uint8Array, rounds: number = TeaCipher.numRounds) {
|
||||
if (key.length != 16) {
|
||||
throw Error("incorrect key size")
|
||||
}
|
||||
if ((rounds & 1) != 0) {
|
||||
throw Error("odd number of rounds specified")
|
||||
}
|
||||
|
||||
const k = new DataView(key.buffer)
|
||||
this.k0 = k.getUint32(0, false)
|
||||
this.k1 = k.getUint32(4, false)
|
||||
this.k2 = k.getUint32(8, false)
|
||||
this.k3 = k.getUint32(12, false)
|
||||
this.rounds = rounds
|
||||
constructor(key: Uint8Array, rounds: number = TeaCipher.numRounds) {
|
||||
if (key.length != 16) {
|
||||
throw Error('incorrect key size');
|
||||
}
|
||||
if ((rounds & 1) != 0) {
|
||||
throw Error('odd number of rounds specified');
|
||||
}
|
||||
|
||||
const k = new DataView(key.buffer);
|
||||
this.k0 = k.getUint32(0, false);
|
||||
this.k1 = k.getUint32(4, false);
|
||||
this.k2 = k.getUint32(8, false);
|
||||
this.k3 = k.getUint32(12, false);
|
||||
this.rounds = rounds;
|
||||
}
|
||||
|
||||
encrypt(dst: DataView, src: DataView) {
|
||||
encrypt(dst: DataView, src: DataView) {
|
||||
let v0 = src.getUint32(0, false);
|
||||
let v1 = src.getUint32(4, false);
|
||||
|
||||
let v0 = src.getUint32(0, false)
|
||||
let v1 = src.getUint32(4, false)
|
||||
|
||||
let sum = 0
|
||||
for (let i = 0; i < this.rounds / 2; i++) {
|
||||
sum = sum + TeaCipher.delta
|
||||
v0 += ((v1 << 4) + this.k0) ^ (v1 + sum) ^ ((v1 >>> 5) + this.k1)
|
||||
v1 += ((v0 << 4) + this.k2) ^ (v0 + sum) ^ ((v0 >>> 5) + this.k3)
|
||||
}
|
||||
|
||||
dst.setUint32(0, v0, false)
|
||||
dst.setUint32(4, v1, false)
|
||||
let sum = 0;
|
||||
for (let i = 0; i < this.rounds / 2; i++) {
|
||||
sum = sum + TeaCipher.delta;
|
||||
v0 += ((v1 << 4) + this.k0) ^ (v1 + sum) ^ ((v1 >>> 5) + this.k1);
|
||||
v1 += ((v0 << 4) + this.k2) ^ (v0 + sum) ^ ((v0 >>> 5) + this.k3);
|
||||
}
|
||||
|
||||
decrypt(dst: DataView, src: DataView) {
|
||||
let v0 = src.getUint32(0, false)
|
||||
let v1 = src.getUint32(4, false)
|
||||
dst.setUint32(0, v0, false);
|
||||
dst.setUint32(4, v1, false);
|
||||
}
|
||||
|
||||
let sum = TeaCipher.delta * this.rounds / 2
|
||||
for (let i = 0; i < this.rounds / 2; i++) {
|
||||
v1 -= ((v0 << 4) + this.k2) ^ (v0 + sum) ^ ((v0 >>> 5) + this.k3)
|
||||
v0 -= ((v1 << 4) + this.k0) ^ (v1 + sum) ^ ((v1 >>> 5) + this.k1)
|
||||
sum -= TeaCipher.delta
|
||||
}
|
||||
dst.setUint32(0, v0, false)
|
||||
dst.setUint32(4, v1, false)
|
||||
decrypt(dst: DataView, src: DataView) {
|
||||
let v0 = src.getUint32(0, false);
|
||||
let v1 = src.getUint32(4, false);
|
||||
|
||||
let sum = (TeaCipher.delta * this.rounds) / 2;
|
||||
for (let i = 0; i < this.rounds / 2; i++) {
|
||||
v1 -= ((v0 << 4) + this.k2) ^ (v0 + sum) ^ ((v0 >>> 5) + this.k3);
|
||||
v0 -= ((v1 << 4) + this.k0) ^ (v1 + sum) ^ ((v1 >>> 5) + this.k1);
|
||||
sum -= TeaCipher.delta;
|
||||
}
|
||||
dst.setUint32(0, v0, false);
|
||||
dst.setUint32(4, v1, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user