Skip to content

Commit

Permalink
refactor - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Sep 26, 2023
1 parent a3f5930 commit 7be4f39
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/crypto/modules/NodeCryptoModule/nodeCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,18 @@ class CryptorHeader {
} else {
throw new PubNubError('decryption error');
}
let headerSize = null;
if (encryptedData.length > pos + 1) {
headerSize = encryptedData[pos];
let metadataLength = null;
if (encryptedData.length >= pos + 1) {
metadataLength = encryptedData[pos];
} else {
throw new PubNubError('decryption error');
}
pos += 1;
if (headerSize === 255 && encryptedData.length >= pos + 2) {
headerSize = new Uint16Array(encryptedData.slice(pos, pos + 3)).reduce((acc, val) => (acc << 8) + val, 0);
if (metadataLength === 255 && encryptedData.length >= pos + 2) {
metadataLength = new Uint16Array(encryptedData.slice(pos, pos + 2)).reduce((acc, val) => (acc << 8) + val, 0);
pos += 2;
}
return new CryptorHeaderV1(identifier.toString('utf8'), headerSize!);
return new CryptorHeaderV1(identifier.toString('utf8'), metadataLength);
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/crypto/modules/WebCryptoModule/webCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,18 @@ class CryptorHeader {
} else {
throw new PubNubError('decryption error');
}
let headerSize = null;
if (encryptedData.byteLength > pos + 1) {
headerSize = (encryptedData as Uint8Array)[pos];
let metadataLength = null;
if (encryptedData.byteLength >= pos + 1) {
metadataLength = (encryptedData as Uint8Array)[pos];
} else {
throw new PubNubError('decryption error');
}
pos += 1;
if (headerSize === 255 && encryptedData.byteLength >= pos + 2) {
headerSize = new Uint16Array(encryptedData.slice(pos, pos + 3)).reduce((acc, val) => (acc << 8) + val, 0);
if (metadataLength === 255 && encryptedData.byteLength >= pos + 2) {
metadataLength = new Uint16Array(encryptedData.slice(pos, pos + 2)).reduce((acc, val) => (acc << 8) + val, 0);
pos += 2;
}
return new CryptorHeaderV1(identifier.toString('utf8'), headerSize!);
return new CryptorHeaderV1(identifier.toString('utf8'), metadataLength);
}
}

Expand Down

0 comments on commit 7be4f39

Please sign in to comment.