Skip to content

Commit

Permalink
feat: Add the first few functions to the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
iainjreid committed Apr 17, 2020
1 parent ca72e14 commit a2f1d58
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/sdk.js
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
})
})
}

0 comments on commit a2f1d58

Please sign in to comment.