Merge pull request #30812 from FlorianDr/master

[pubnub] Add encrypt and decrypt methods
This commit is contained in:
Nathan Shively-Sanders
2018-12-03 09:16:59 -08:00
committed by GitHub
2 changed files with 34 additions and 1 deletions
+22 -1
View File
@@ -1,8 +1,9 @@
// Type definitions for pubnub 4.0
// Project: https://github.com/pubnub/javascript
// Definitions by: bitbankinc <https://github.com/bitbankinc>, rollymaduk <https://github.com/rollymaduk>, vitosamson <https://github.com/vitosamson>
// Definitions by: bitbankinc <https://github.com/bitbankinc>, rollymaduk <https://github.com/rollymaduk>, vitosamson <https://github.com/vitosamson>, FlorianDr <https://github.com/FlorianDr>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// @see https://www.pubnub.com/docs/web-javascript/api-reference-configuration
// TypeScript Version: 2.2
declare class Pubnub {
constructor(config: Pubnub.PubnubConfig);
@@ -88,6 +89,18 @@ declare class Pubnub {
setState(
params: Pubnub.SetStateParameters
): Promise<Pubnub.SetStateResponse>;
encrypt(
data: string,
customCipherKey?: string,
options?: Pubnub.CryptoParameters,
): any;
decrypt(
data: object,
customCipherKey?: string,
options?: Pubnub.CryptoParameters
): any;
}
declare namespace Pubnub {
@@ -296,6 +309,14 @@ declare namespace Pubnub {
};
}
// encrypt & decrypt
interface CryptoParameters {
encryptKey?: boolean;
keyEncoding?: string;
keyLength?: number;
mode?: string;
}
interface Categories {
PNNetworkUpCategory: string;
PNNetworkDownCategory: string;
+12
View File
@@ -86,3 +86,15 @@ pubnub.setState({ channels: [] }, (status, res) => {
pubnub.setState({ channels: [] }).then(res => {
console.log(res.state);
});
const cryptoOptions = {
encryptKey: true,
keyEncoding: 'utf8',
keyLength: 256,
mode: 'cbc'
};
const mySecret = {
message: 'Hi!',
};
pubnub.decrypt(mySecret, undefined, cryptoOptions);
pubnub.encrypt('egrah5rwgrehwqh5eh3hwfwef', undefined, cryptoOptions);