Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,662 changes: 1,046 additions & 1,616 deletions dist/web/pubnub.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs-snippets/basic-usage/miscellaneous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const msgContent = 'This is the data I wish to encrypt.';
console.log(`Original Message: ${msgContent}`);

// Encrypt the message
const encryptedMessage = cryptoModule.encrypt(JSON.stringify(msgContent));
const encryptedMessage = await cryptoModule.encrypt(JSON.stringify(msgContent));
console.log('message encrypted successfully');
// snippet.end

Expand All @@ -34,7 +34,7 @@ const encryptedFile = await pubnub.encryptFile(file);

const encrypted = '..';
// snippet.decryptBasicUsage
const decrypted = pubnub.decrypt(encrypted); // Pass the encrypted data as the first parameter in decrypt Method
const decrypted = await pubnub.decrypt(encrypted); // Pass the encrypted data as the first parameter in decrypt Method
// snippet.end

// snippet.decryptFileBasicUsage
Expand Down
128 changes: 128 additions & 0 deletions lib/core/components/cryptography/bytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"use strict";
/**
* Byte helpers shared by cryptors and crypto primitives.
*
* @internal
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeUtf8Strict = exports.isWellFormedUtf8 = exports.toArrayBuffer = exports.bytesToHex = void 0;
/**
* Lazily created UTF-8 decoder.
*
* **Note:** Not created at module load and doesn't use `fatal` option because some polyfills (for example
* `fast-text-encoding` used on React Native) throw when it is requested. Input is validated by
* {@link isWellFormedUtf8} instead, so the decoder only ever receives well-formed data.
*/
let decoder;
/**
* Whether {@link decoder} strips a leading BOM despite `ignoreBOM` option (detected once, on first use).
*/
let decoderStripsBOM = false;
/**
* Convert bytes to lowercase HEX string.
*
* @param bytes - Source bytes.
*
* @returns HEX encoded bytes.
*
* @internal
*/
const bytesToHex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
exports.bytesToHex = bytesToHex;
/**
* Copy bytes into standalone {@link ArrayBuffer}.
*
* @param bytes - Source bytes (may be a view into a larger buffer).
*
* @returns {@link ArrayBuffer} which contains only source bytes.
*
* @internal
*/
const toArrayBuffer = (bytes) => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
exports.toArrayBuffer = toArrayBuffer;
/**
* Check whether bytes are well-formed UTF-8.
*
* Implements Unicode "Well-Formed UTF-8 Byte Sequences" (Table 3-7): rejects stray continuation bytes, overlong
* forms (`C0`, `C1`, `E0 80-9F`, `F0 80-8F`), surrogates (`ED A0-BF`), code points above U+10FFFF (`F4 90-BF`,
* `F5-FF`) and truncated sequences. Same rules as CryptoJS `Utf8.stringify` (`decodeURIComponent`).
*
* @param bytes - Bytes which should be checked.
*
* @returns `true` if bytes are well-formed UTF-8.
*
* @internal
*/
const isWellFormedUtf8 = (bytes) => {
const length = bytes.length;
let index = 0;
while (index < length) {
const lead = bytes[index];
if (lead < 0x80) {
index += 1;
continue;
}
let continuationBytes;
// Allowed range of the first continuation byte (narrower for some lead bytes).
let lower = 0x80;
let upper = 0xbf;
if (lead >= 0xc2 && lead <= 0xdf)
continuationBytes = 1;
else if (lead >= 0xe0 && lead <= 0xef) {
continuationBytes = 2;
if (lead === 0xe0)
lower = 0xa0;
else if (lead === 0xed)
upper = 0x9f;
}
else if (lead >= 0xf0 && lead <= 0xf4) {
continuationBytes = 3;
if (lead === 0xf0)
lower = 0x90;
else if (lead === 0xf4)
upper = 0x8f;
}
else
return false;
if (index + continuationBytes >= length)
return false;
const first = bytes[index + 1];
if (first < lower || first > upper)
return false;
for (let offset = 2; offset <= continuationBytes; offset++) {
const next = bytes[index + offset];
if (next < 0x80 || next > 0xbf)
return false;
}
index += continuationBytes + 1;
}
return true;
};
exports.isWellFormedUtf8 = isWellFormedUtf8;
/**
* Strictly decode UTF-8 bytes.
*
* Behaves like CryptoJS `Utf8.stringify` used by previous SDK versions: malformed sequences are rejected instead of
* being replaced, and a leading BOM is preserved (regular decoders silently strip it). Result doesn't depend on the
* platform `TextDecoder` implementation or polyfill.
*
* @param bytes - UTF-8 encoded bytes.
*
* @returns Decoded string.
*
* @throws Error if bytes aren't a valid UTF-8 sequence.
*
* @internal
*/
const decodeUtf8Strict = (bytes) => {
if (!(0, exports.isWellFormedUtf8)(bytes))
throw new Error('Malformed UTF-8 data');
if (!decoder) {
decoder = new TextDecoder('utf-8', { ignoreBOM: true });
decoderStripsBOM = decoder.decode(new Uint8Array([0xef, 0xbb, 0xbf])).length === 0;
}
const decoded = decoder.decode(bytes);
const hasBOM = bytes.length >= 3 && bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf;
return hasBOM && decoderStripsBOM ? `\uFEFF${decoded}` : decoded;
};
exports.decodeUtf8Strict = decodeUtf8Strict;
Loading
Loading