-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add the first few functions to the SDK
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict' | ||
|
||
const keyOptions = [ | ||
{ | ||
name: 'HMAC', | ||
hash: { name: 'SHA-256' } | ||
}, | ||
true, | ||
['sign'] | ||
] | ||
|
||
/** | ||
* Create a new `CryptoKey` object that can be used to sign values. | ||
* | ||
* @returns {CryptoKey} | ||
*/ | ||
export.generateKey = () => { | ||
return crypto.subtle.generateKey(...keyOptions) | ||
} | ||
|
||
/** | ||
* Import an `ArrayBuffer` object as a raw key and convert it into a usable `CryptoKey`. | ||
* | ||
* @param key {ArrayBuffer} | ||
* | ||
* @returns {CryptoKey} | ||
*/ | ||
export.importKey = (key) => { | ||
return crypto.subtle.importKey('raw', key, ...keyOptions) | ||
} | ||
|
||
/** | ||
* Export the provided `CryptoKey` and convert it into an `ArrayBuffer` object. | ||
* | ||
* @param key {CryptoKey} | ||
* | ||
* @returns {ArrayBuffer} | ||
*/ | ||
export.exportKey = (key) => { | ||
return crypto.subtle.exportKey('raw', key) | ||
} | ||
|
||
/** | ||
* Generate a ticket for the required epoch. | ||
* | ||
* @param key {CryptoKey} | ||
* @param timestamp {Number} | ||
* | ||
* @returns {Number} | ||
*/ | ||
export.createTicket = (key, timestamp) => { | ||
// An ArrayBuffer representation of the timestamp | ||
const _timestamp = (new TextEncoder()).encode(timestamp).buffer | ||
|
||
return crypto.subtle.sign(keyOptions[0], key, _timestamp).then((key) => { | ||
return (new Uint8Array(key)).reduce((acc, int) => { | ||
return (acc + int) % 1000 | ||
}) | ||
}) | ||
} | ||
|