kuzzleio / kuzzle-vault Goto Github PK
View Code? Open in Web Editor NEWSecurely share secrets for your application with JSON and cryptography
License: Apache License 2.0
Securely share secrets for your application with JSON and cryptography
License: Apache License 2.0
All the properties of all types of objects to be encrypted should be encrypted.
If a property of the object to be encrypted is not a string, the property doest not get encrypted and it's not added to the encrypted object.
In the function encryptObject, the function should check if the property is something else than a string.
if it is, cast the property to string and add the type at the end of the string. This way, the property will be encrypted as a string and when decrypting, the type can be restored.
For example:
// Before encryption
{
"name": "John Cena",
"age": 30,
"active": true
}
// What gets encrypted
{
"name": "John Cena",
"age": "30+number",
"active": "true+boolean"
}
So the encryptObject would look like this:
encryptObject (secrets: any): any {
const encryptedSecrets: any = {};
for (const key of Object.keys(secrets)) {
const value: string|any = secrets[key];
if (value && typeof value === 'object' && !Array.isArray(value)) {
encryptedSecrets[key] = this.encryptObject(value);
}
else if (typeof value === 'string') {
encryptedSecrets[key] = this.encryptString(value);
}
else if (['boolean','number'].includes(typeof value)) {
const toBeEncrypted = `${value.toString()}+${typeof value}`;
encryptedSecrets[key] = this.encryptString(toBeEncrypted);
}
}
return encryptedSecrets;
}
ant the decyptString would look like this:
decryptString (encrypted: string): string {
const [ encryptedData, ivHex ] = encrypted.split('.');
if (encryptedData.length === 0) {
throw new Error(`Invalid encrypted string format "${encryptedData}.${ivHex}"`);
}
if (ivHex.length !== 32) {
throw new Error(`Invalid IV size. (${ivHex.length}, expected 32)`);
}
const iv = Buffer.from(ivHex, 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', this.vaultKeyHash, iv);
try {
const deciphered: string = decipher.update(encryptedData, 'hex', 'utf8') + decipher.final('utf8');
const splited = deciphered.split('+');
if(splited.length > 1){
const originalType = splited.pop();
if(originalType === 'number'){
return Number(splited.join('+'))
};
if(originalType === 'boolean'){
return Boolean(splited.join('+'))
};
}
return deciphered;
}
catch (error) {
if (error.message.includes('bad decrypt')) {
throw new Error('Cannot decrypt encrypted value with the provided key');
}
throw new Error(`Encrypted input value format is not a valid: ${error.message}`);
}
}
{
"name": "John Cena",
"age": 30,
"active": true
}
kourou vault:encrypt secrets.json --vault-key password
Kuzzle version: 2.18.1
Node.js version: 16.14.2
SDK version: -
Kourous version: 0.22.0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.