-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1299 from ably/1292-use-web-crypto-for-encrypting…
…-and-decrypting Use Web Crypto API for encrypting and decrypting
- Loading branch information
Showing
16 changed files
with
259 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import { TypedArray } from './IPlatformConfig'; | ||
|
||
export default interface IBufferUtils<Bufferlike, Output, ToBufferOutput, ComparableBuffer, WordArrayLike> { | ||
export default interface IBufferUtils<Bufferlike, Output, ToBufferOutput, WordArrayLike> { | ||
base64CharSet: string; | ||
hexCharSet: string; | ||
isBuffer: (buffer: unknown) => buffer is Bufferlike; | ||
isArrayBuffer: (buffer: unknown) => buffer is ArrayBuffer; | ||
isWordArray: (val: unknown) => val is WordArrayLike; | ||
// On browser this returns a Uint8Array, on node a Buffer | ||
toBuffer: (buffer: Bufferlike) => ToBufferOutput; | ||
toArrayBuffer: (buffer: Bufferlike) => ArrayBuffer; | ||
toArrayBuffer: (buffer: Bufferlike | WordArrayLike) => ArrayBuffer; | ||
base64Encode: (buffer: Bufferlike) => string; | ||
base64Decode: (string: string) => Output; | ||
hexEncode: (buffer: Bufferlike) => string; | ||
hexDecode: (string: string) => Output; | ||
utf8Encode: (string: string) => Output; | ||
utf8Decode: (buffer: Bufferlike) => string; | ||
bufferCompare: (buffer1: ComparableBuffer, buffer2: ComparableBuffer) => number; | ||
areBuffersEqual: (buffer1: Bufferlike, buffer2: Bufferlike) => boolean; | ||
byteLength: (buffer: Bufferlike) => number; | ||
typedArrayToBuffer: (typedArray: TypedArray) => Bufferlike; | ||
arrayBufferViewToBuffer: (arrayBufferView: ArrayBufferView) => Bufferlike; | ||
toWordArray: (buffer: Bufferlike | number[]) => WordArrayLike; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
import msgpack from '../web/lib/util/msgpack'; | ||
import { parse as parseBase64 } from 'crypto-js/build/enc-base64'; | ||
import { IPlatformConfig } from '../../common/types/IPlatformConfig'; | ||
import BufferUtils from '../web/lib/util/bufferutils'; | ||
|
||
const Platform: IPlatformConfig = { | ||
agent: 'reactnative', | ||
logTimestamps: true, | ||
noUpgrade: false, | ||
binaryType: 'arraybuffer', | ||
WebSocket: WebSocket, | ||
xhrSupported: true, | ||
allowComet: true, | ||
streamingSupported: true, | ||
useProtocolHeartbeats: true, | ||
createHmac: null, | ||
msgpack: msgpack, | ||
supportsBinary: !!(typeof TextDecoder !== 'undefined' && TextDecoder), | ||
preferBinary: false, | ||
ArrayBuffer: typeof ArrayBuffer !== 'undefined' && ArrayBuffer, | ||
atob: global.atob, | ||
nextTick: function (f: Function) { | ||
setTimeout(f, 0); | ||
}, | ||
addEventListener: null, | ||
inspect: JSON.stringify, | ||
stringByteSize: function (str: string) { | ||
/* str.length will be an underestimate for non-ascii strings. But if we're | ||
* in a browser too old to support TextDecoder, not much we can do. Better | ||
* to underestimate, so if we do go over-size, the server will reject the | ||
* message */ | ||
return (typeof TextDecoder !== 'undefined' && new TextEncoder().encode(str).length) || str.length; | ||
}, | ||
TextEncoder: global.TextEncoder, | ||
TextDecoder: global.TextDecoder, | ||
Promise: global.Promise, | ||
getRandomWordArray: (function (RNRandomBytes) { | ||
return function (byteLength: number, callback: (err: Error | null, result: CryptoJS.lib.WordArray | null) => void) { | ||
RNRandomBytes.randomBytes(byteLength, function (err: Error | null, base64String: string | null) { | ||
callback(err, base64String ? parseBase64(base64String) : null); | ||
}); | ||
}; | ||
// Installing @types/react-native would fix this but conflicts with @types/node | ||
// See https://github.com/DefinitelyTyped/DefinitelyTyped/issues/15960 | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
})(require('react-native').NativeModules.RNRandomBytes), | ||
}; | ||
|
||
export default Platform; | ||
export default function (bufferUtils: typeof BufferUtils): IPlatformConfig { | ||
return { | ||
agent: 'reactnative', | ||
logTimestamps: true, | ||
noUpgrade: false, | ||
binaryType: 'arraybuffer', | ||
WebSocket: WebSocket, | ||
xhrSupported: true, | ||
allowComet: true, | ||
streamingSupported: true, | ||
useProtocolHeartbeats: true, | ||
createHmac: null, | ||
msgpack: msgpack, | ||
supportsBinary: !!(typeof TextDecoder !== 'undefined' && TextDecoder), | ||
preferBinary: false, | ||
ArrayBuffer: typeof ArrayBuffer !== 'undefined' && ArrayBuffer, | ||
atob: global.atob, | ||
nextTick: function (f: Function) { | ||
setTimeout(f, 0); | ||
}, | ||
addEventListener: null, | ||
inspect: JSON.stringify, | ||
stringByteSize: function (str: string) { | ||
/* str.length will be an underestimate for non-ascii strings. But if we're | ||
* in a browser too old to support TextDecoder, not much we can do. Better | ||
* to underestimate, so if we do go over-size, the server will reject the | ||
* message */ | ||
return (typeof TextDecoder !== 'undefined' && new TextEncoder().encode(str).length) || str.length; | ||
}, | ||
TextEncoder: global.TextEncoder, | ||
TextDecoder: global.TextDecoder, | ||
Promise: global.Promise, | ||
getRandomArrayBuffer: (function (RNRandomBytes) { | ||
return function (byteLength: number, callback: (err: Error | null, result: ArrayBuffer | null) => void) { | ||
RNRandomBytes.randomBytes(byteLength, function (err: Error | null, base64String: string | null) { | ||
callback(err, base64String ? bufferUtils.toArrayBuffer(bufferUtils.base64Decode(base64String)) : null); | ||
}); | ||
}; | ||
// Installing @types/react-native would fix this but conflicts with @types/node | ||
// See https://github.com/DefinitelyTyped/DefinitelyTyped/issues/15960 | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
})(require('react-native').NativeModules.RNRandomBytes), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.