Created
October 14, 2017 20:56
-
-
Save Duske/7d08d4ed5fef5c009a73e838664876c9 to your computer and use it in GitHub Desktop.
Helper functions to deal with web push encryption stuff
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
function generateKey(keyName, subscription) { | |
var rawKey; | |
rawKey = subscription.getKey ? subscription.getKey(keyName) : ''; | |
return rawKey ? | |
btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : | |
''; | |
} | |
export function generatePublicKey(subscription) { | |
return Promise.resolve(generateKey('p256dh', subscription)); | |
} | |
export function generateAuthKey(subscription) { | |
return Promise.resolve(generateKey('auth', subscription)); | |
} | |
export function urlBase64ToUint8Array(base64String) { | |
const padding = '='.repeat((4 - base64String.length % 4) % 4); | |
const base64 = (base64String + padding) | |
.replace(/\-/g, '+') | |
.replace(/_/g, '/'); | |
const rawData = window.atob(base64); | |
const outputArray = new Uint8Array(rawData.length); | |
for (let i = 0; i < rawData.length; ++i) { | |
outputArray[i] = rawData.charCodeAt(i); | |
} | |
return outputArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment