This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
225 additions
and
38 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 |
---|---|---|
|
@@ -103,3 +103,4 @@ cloud_config/ | |
|
||
# Test Files | ||
testFiles/ | ||
devKey.js |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,25 @@ | ||
const { generateAPIKey } = require("./utils/generateAPIKey") | ||
const { sha384 } = require("./utils/sha384") | ||
const { sign } = require('./utils/sign.js') | ||
|
||
function createDataWrite(_data, _store, _secret, _moat, _privateJWK) { | ||
const timestamp = Date.now() | ||
const hash = sha384(_data+timestamp.toString()+_secret) | ||
const queryID = generateAPIKey(64) | ||
const signature = sign({data : _data, | ||
timestamp: timestamp, | ||
hash: hash, | ||
queryID: queryID, | ||
}, _privateJWK) | ||
return { | ||
data : _data, | ||
timestamp: timestamp, | ||
hash: hash, | ||
queryID: queryID, | ||
signature: signature, | ||
store: _store, | ||
moat: _moat | ||
} | ||
} | ||
|
||
module.exports = {createDataWrite} |
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
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
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
const generateAPIKey = () => { | ||
const generateAPIKey = (_length=32) => { | ||
const validChars = | ||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890,.<>/?;:[]{}|+=_-)(*&^%$#@!~'; | ||
let salt = ''; | ||
for (let i = 0; i < 32; i++) { | ||
for (let i = 0; i < _length; i++) { | ||
const randomElement = Math.floor(Math.random() * validChars.length); | ||
salt = salt + validChars[randomElement]; | ||
} | ||
return salt; | ||
}; | ||
|
||
module.exports = {generateAPIKey} |
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,67 @@ | ||
const rs = require('jsrsasign') | ||
const crypto = require('crypto') | ||
const generateKeyPairNode = require('./generateKeyPairNode') | ||
|
||
const getPublicJWKFromPrivateJWK = (_privateJWK) => { | ||
//This function takes a private key and returns a public JWK | ||
let pubJWK = { | ||
kty: _privateJWK.kty, | ||
n: _privateJWK.n, | ||
e: _privateJWK.e, | ||
}; | ||
return pubJWK; | ||
}; | ||
|
||
|
||
|
||
const generateKeyPair = async () => { | ||
|
||
if (typeof crypto.generateKeyPair == "function") { | ||
const keys = await generateKeyPairNode() | ||
return keys | ||
} | ||
|
||
async function WrapperFunction() { | ||
if (typeof window === 'object') { | ||
if (typeof window.crypto === 'object') { | ||
try { | ||
let keyPair = await window.crypto.subtle.generateKey( | ||
{ | ||
name: 'RSA-PSS', | ||
modulusLength: 4096, //can be 1024, 2048, or 4096 | ||
publicExponent: new Uint8Array([0x01, 0x00, 0x01]), | ||
hash: { name: 'SHA-256' }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512" | ||
}, | ||
true, //whether the key is extractable (i.e. can be used in exportKey) | ||
['sign', 'verify'] //can be any combination of "sign" and "verify" | ||
); | ||
let jwk = await window.crypto.subtle.exportKey('jwk', keyPair.privateKey); | ||
let privateKey = rs.KEYUTIL.getKey(jwk); | ||
const rsaJWK = rs.KEYUTIL.getJWKFromKey(privateKey); | ||
return { pubKey: getPublicJWKFromPrivateJWK(rsaJWK), privateKey: rsaJWK }; | ||
} | ||
catch ({ message } ) { | ||
return { | ||
status: 400, | ||
message | ||
}; | ||
} | ||
//return keyPair; | ||
} | ||
} //Test for node | ||
else { | ||
console.log('window.crypto not available. Key generation may take a while...'); | ||
try { | ||
let keyPair = await rs.KEYUTIL.generateKeypair('RSA', 4096); | ||
const rsaJWK = await rs.KEYUTIL.getJWKFromKey(keyPair.prvKeyObj); | ||
return { pubKey: getPublicJWKFromPrivateJWK(rsaJWK), privateKey: rsaJWK }; | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
} | ||
const retVal = await WrapperFunction() | ||
return retVal | ||
} | ||
|
||
module.exports = {generateKeyPair} |
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,24 @@ | ||
const crypto = require('crypto') | ||
const pem2jwk = require('pem-jwk').pem2jwk | ||
|
||
async function generateKeyPairNode() { | ||
if (typeof crypto.generateKeyPair == "function") { | ||
//running in nodejs | ||
const keys = crypto.generateKeyPairSync('rsa', { | ||
modulusLength: 4096, // options | ||
publicExponent: 0x10001, | ||
publicKeyEncoding: { | ||
type: 'pkcs1', | ||
format: 'pem' | ||
}, | ||
privateKeyEncoding: { | ||
type: 'pkcs1', | ||
format: 'pem' | ||
} | ||
} | ||
) | ||
return {publicKey: pem2jwk(keys.publicKey), privateKey: pem2jwk(keys.privateKey)} | ||
} else {throw new Error('Not in nodejs')} | ||
} | ||
|
||
module.exports = generateKeyPairNode |
Oops, something went wrong.