Skip to content

Commit 707075d

Browse files
committed
Merge pull request #149 from eoger/strings2buffers
Convert strings to Buffers before encryption
2 parents 994009d + 94bb2bb commit 707075d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ function setGCMAPIKey(apiKey) {
5656

5757
// Old standard, Firefox 44+.
5858
function encryptOld(userPublicKey, payload) {
59+
if (typeof payload === 'string' || payload instanceof String) {
60+
payload = new Buffer(payload);
61+
}
5962
var localCurve = crypto.createECDH('prime256v1');
6063

6164
var localPublicKey = localCurve.generateKeys();
@@ -82,6 +85,9 @@ function encryptOld(userPublicKey, payload) {
8285

8386
// New standard, Firefox 46+ and Chrome 50+.
8487
function encrypt(userPublicKey, userAuth, payload) {
88+
if (typeof payload === 'string' || payload instanceof String) {
89+
payload = new Buffer(payload);
90+
}
8591
var localCurve = crypto.createECDH('prime256v1');
8692
var localPublicKey = localCurve.generateKeys();
8793

@@ -160,12 +166,12 @@ function sendNotification(endpoint, params) {
160166
var cryptoHeaderName;
161167
if (userAuth) {
162168
// Use the new standard if userAuth is defined (Firefox 46+ and Chrome 50+).
163-
encrypted = encrypt(userPublicKey, userAuth, new Buffer(payload));
169+
encrypted = encrypt(userPublicKey, userAuth, payload);
164170
encodingHeader = 'aesgcm';
165171
cryptoHeaderName = 'Crypto-Key';
166172
} else {
167173
// Use the old standard if userAuth isn't defined (up to Firefox 45).
168-
encrypted = encryptOld(userPublicKey, new Buffer(payload));
174+
encrypted = encryptOld(userPublicKey, payload);
169175
encodingHeader = 'aesgcm128';
170176
cryptoHeaderName = 'Encryption-Key';
171177
}

0 commit comments

Comments
 (0)