mirror of
https://github.com/amark/gun.git
synced 2025-06-06 14:16:44 +00:00
WIP: aeskey from jwk instead of raw for RN
This commit is contained in:
parent
0311969060
commit
0b31971678
12
sea.js
12
sea.js
@ -221,6 +221,13 @@
|
|||||||
if(d){ jwk.d = d }
|
if(d){ jwk.d = d }
|
||||||
return jwk;
|
return jwk;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
s.keyTojwk = function(keyBytes) {
|
||||||
|
var jwkKeyString = keyBytes.toString('base64')
|
||||||
|
jwkKeyString = jwkKeyString.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=/g, "");
|
||||||
|
jwkKeyString = { kty: "oct", k: jwkKeyString, ext: false, alg: "A256GCM"};
|
||||||
|
return jwkKeyString
|
||||||
|
}
|
||||||
s.recall = {
|
s.recall = {
|
||||||
validity: 12 * 60 * 60, // internally in seconds : 12 hours
|
validity: 12 * 60 * 60, // internally in seconds : 12 hours
|
||||||
hook: function(props){ return props } // { iat, exp, alias, remember } // or return new Promise((resolve, reject) => resolve(props)
|
hook: function(props){ return props } // { iat, exp, alias, remember } // or return new Promise((resolve, reject) => resolve(props)
|
||||||
@ -496,6 +503,7 @@
|
|||||||
|
|
||||||
;USE(function(module){
|
;USE(function(module){
|
||||||
var shim = USE('./shim');
|
var shim = USE('./shim');
|
||||||
|
var S = USE('./settings');
|
||||||
var sha256hash = USE('./sha256');
|
var sha256hash = USE('./sha256');
|
||||||
|
|
||||||
const importGen = async (key, salt, opt) => {
|
const importGen = async (key, salt, opt) => {
|
||||||
@ -503,7 +511,8 @@
|
|||||||
var opt = opt || {};
|
var opt = opt || {};
|
||||||
const combo = key + (salt || shim.random(8)).toString('utf8'); // new
|
const combo = key + (salt || shim.random(8)).toString('utf8'); // new
|
||||||
const hash = shim.Buffer.from(await sha256hash(combo), 'binary')
|
const hash = shim.Buffer.from(await sha256hash(combo), 'binary')
|
||||||
return await shim.subtle.importKey('raw', new Uint8Array(hash), opt.name || 'AES-GCM', false, ['encrypt', 'decrypt'])
|
const jwkHash = S.keyTojwk(hash)
|
||||||
|
return await shim.subtle.importKey('jwk', jwkHash, {name:'AES-GCM'}, false, ['encrypt', 'decrypt'])
|
||||||
}
|
}
|
||||||
module.exports = importGen;
|
module.exports = importGen;
|
||||||
})(USE, './aeskey');
|
})(USE, './aeskey');
|
||||||
@ -650,6 +659,7 @@
|
|||||||
SEA.verify = USE('./verify');
|
SEA.verify = USE('./verify');
|
||||||
SEA.encrypt = USE('./encrypt');
|
SEA.encrypt = USE('./encrypt');
|
||||||
SEA.decrypt = USE('./decrypt');
|
SEA.decrypt = USE('./decrypt');
|
||||||
|
SEA.aeskey = USE('./aeskey');
|
||||||
|
|
||||||
SEA.random = SEA.random || shim.random;
|
SEA.random = SEA.random || shim.random;
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
var shim = require('./shim');
|
var shim = require('./shim');
|
||||||
|
var S = require('./settings');
|
||||||
var sha256hash = require('./sha256');
|
var sha256hash = require('./sha256');
|
||||||
|
|
||||||
const importGen = async (key, salt, opt) => {
|
const importGen = async (key, salt, opt) => {
|
||||||
@ -7,7 +8,8 @@
|
|||||||
var opt = opt || {};
|
var opt = opt || {};
|
||||||
const combo = key + (salt || shim.random(8)).toString('utf8'); // new
|
const combo = key + (salt || shim.random(8)).toString('utf8'); // new
|
||||||
const hash = shim.Buffer.from(await sha256hash(combo), 'binary')
|
const hash = shim.Buffer.from(await sha256hash(combo), 'binary')
|
||||||
return await shim.subtle.importKey('raw', new Uint8Array(hash), opt.name || 'AES-GCM', false, ['encrypt', 'decrypt'])
|
const jwkHash = S.keyTojwk(hash)
|
||||||
|
return await shim.subtle.importKey('jwk', jwkHash, {name:'AES-GCM'}, false, ['encrypt', 'decrypt'])
|
||||||
}
|
}
|
||||||
module.exports = importGen;
|
module.exports = importGen;
|
||||||
|
|
@ -7,6 +7,7 @@
|
|||||||
SEA.verify = require('./verify');
|
SEA.verify = require('./verify');
|
||||||
SEA.encrypt = require('./encrypt');
|
SEA.encrypt = require('./encrypt');
|
||||||
SEA.decrypt = require('./decrypt');
|
SEA.decrypt = require('./decrypt');
|
||||||
|
SEA.aeskey = require('./aeskey');
|
||||||
|
|
||||||
SEA.random = SEA.random || shim.random;
|
SEA.random = SEA.random || shim.random;
|
||||||
|
|
||||||
|
@ -18,6 +18,13 @@
|
|||||||
if(d){ jwk.d = d }
|
if(d){ jwk.d = d }
|
||||||
return jwk;
|
return jwk;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
s.keyTojwk = function(keyBytes) {
|
||||||
|
var jwkKeyString = keyBytes.toString('base64')
|
||||||
|
jwkKeyString = jwkKeyString.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=/g, "");
|
||||||
|
jwkKeyString = { kty: "oct", k: jwkKeyString, ext: false, alg: "A256GCM"};
|
||||||
|
return jwkKeyString
|
||||||
|
}
|
||||||
s.recall = {
|
s.recall = {
|
||||||
validity: 12 * 60 * 60, // internally in seconds : 12 hours
|
validity: 12 * 60 * 60, // internally in seconds : 12 hours
|
||||||
hook: function(props){ return props } // { iat, exp, alias, remember } // or return new Promise((resolve, reject) => resolve(props)
|
hook: function(props){ return props } // { iat, exp, alias, remember } // or return new Promise((resolve, reject) => resolve(props)
|
||||||
|
19
sea/shim.js
19
sea/shim.js
@ -6,24 +6,31 @@
|
|||||||
|
|
||||||
if(SEA.window){
|
if(SEA.window){
|
||||||
api.crypto = window.crypto || window.msCrypto;
|
api.crypto = window.crypto || window.msCrypto;
|
||||||
|
if(!api.crypto) {
|
||||||
|
api.crypto = require('isomorphic-webcrypto');
|
||||||
|
}
|
||||||
api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle;
|
api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle;
|
||||||
api.TextEncoder = window.TextEncoder;
|
api.TextEncoder = window.TextEncoder;
|
||||||
api.TextDecoder = window.TextDecoder;
|
api.TextDecoder = window.TextDecoder;
|
||||||
api.random = (len) => Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len))))
|
api.random = (len) => Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len))))
|
||||||
}
|
}
|
||||||
|
if(!api.TextDecoder)
|
||||||
|
{
|
||||||
|
const { TextEncoder, TextDecoder } = require('text-encoding')
|
||||||
|
api.TextDecoder = TextDecoder
|
||||||
|
api.TextEncoder = TextEncoder
|
||||||
|
}
|
||||||
if(!api.crypto){try{
|
if(!api.crypto){try{
|
||||||
var crypto = require('crypto', 1);
|
var crypto = require('crypto', 1);
|
||||||
const { TextEncoder, TextDecoder } = require('text-encoding', 1)
|
|
||||||
Object.assign(api, {
|
Object.assign(api, {
|
||||||
crypto,
|
crypto,
|
||||||
//subtle,
|
|
||||||
TextEncoder,
|
|
||||||
TextDecoder,
|
|
||||||
random: (len) => Buffer.from(crypto.randomBytes(len))
|
random: (len) => Buffer.from(crypto.randomBytes(len))
|
||||||
});
|
});
|
||||||
//try{
|
//try{
|
||||||
const WebCrypto = require('node-webcrypto-ossl', 1);
|
// const WebCrypto = require('node-webcrypto-ossl', 1);
|
||||||
api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH
|
// api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH
|
||||||
|
const isocrypto = require('isomorphic-webcrypto');
|
||||||
|
api.ossl = api.subtle = isocrypto.subtle
|
||||||
//}catch(e){
|
//}catch(e){
|
||||||
//console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
|
//console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
|
||||||
//}
|
//}
|
||||||
|
@ -37,7 +37,12 @@ describe('SEA', function(){
|
|||||||
var gun;
|
var gun;
|
||||||
var pub;
|
var pub;
|
||||||
describe('Utility', function(){
|
describe('Utility', function(){
|
||||||
|
it('generates aeskey from jwk', function(done) {
|
||||||
|
SEA.aeskey('x','x').then(k => {
|
||||||
|
expect(k.data.toString('base64')).to.be('Xd6JaIf2dUybFb/jpEGuSAbfL96UABMR4IvxEGIuC74=')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
it('quickstart', function(done){
|
it('quickstart', function(done){
|
||||||
SEA.pair(function(pair){
|
SEA.pair(function(pair){
|
||||||
SEA.encrypt('hello self', pair, function(enc){
|
SEA.encrypt('hello self', pair, function(enc){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user