@evervault/sdk
Advanced tools
Comparing version 0.5.1 to 0.6.0
{ | ||
"name": "@evervault/sdk", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "evervault Browser SDK", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -41,13 +41,18 @@ /** @format */ | ||
.then((encString) => { | ||
const splitEncString = encString.split(':'); | ||
const splitEncString = encString.split('.'); | ||
chai.assert.lengthOf( | ||
splitEncString, | ||
4, | ||
'encrypted string should split into 5 parts' | ||
2, | ||
'encrypted string should split into 2 parts' | ||
); | ||
chai.assert.equal( | ||
splitEncString[0], | ||
'enc', | ||
'first part of encrypted string is not equal to enc' | ||
splitEncString[0].substr(0,3), | ||
'eyJ', | ||
'first part of encrypted string is not equal to eyJ' | ||
); | ||
chai.assert.equal( | ||
splitEncString[1].substr(0,3), | ||
'eyJ', | ||
'first part of encrypted string is not equal to eyJ' | ||
); | ||
done(); | ||
@@ -58,6 +63,8 @@ }) | ||
it('should decrypt and encrypted string', function (done) { | ||
it('should decrypt an encrypted string', function (done) { | ||
evervault | ||
.encrypt(testEncStr) | ||
.then((str) => evervault.decrypt(str)) | ||
.then((str) => | ||
evervault.decrypt(str) | ||
) | ||
.then((decStr) => { | ||
@@ -64,0 +71,0 @@ chai.assert.equal( |
77
utils.js
@@ -50,2 +50,22 @@ /** @format */ | ||
static base64UrlEncode(data) { | ||
data = window.btoa(data); | ||
return data.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''); | ||
} | ||
static base64UrlDecode(data) { | ||
data = data.replace(/-/g, '+').replace(/_/g, '/'); | ||
const pad = data.length % 4; | ||
if (pad) { | ||
if (pad === 1) { | ||
throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding'); | ||
} | ||
data += new Array(5 - pad).join('='); | ||
} | ||
return window.atob(data); | ||
} | ||
static ensureString(data) { | ||
@@ -101,11 +121,15 @@ const getString = (data) => { | ||
static parseEvervaultString(str) { | ||
const valueArr = str.split(':'); | ||
if (valueArr.length < 4) { | ||
const valueArr = str.split('.') | ||
const parsedHeader = JSON.parse(this.base64UrlDecode(valueArr[0])); | ||
const parsedBody = JSON.parse(this.base64UrlDecode(valueArr[1])); | ||
if (!parsedHeader.type || parsedHeader.type !== 'evervault_string' || !parsedBody.encryptedData) { | ||
throw new Error('String does not match expected structure'); | ||
} | ||
return { | ||
prefix: valueArr[0], | ||
version: valueArr[1], | ||
iv: valueArr[2], | ||
browserData: valueArr[3], | ||
prefix: parsedHeader.type, | ||
version: parsedHeader.version, | ||
iv: parsedBody.keyIv, | ||
browserData: parsedBody.encryptedData, | ||
}; | ||
@@ -115,4 +139,12 @@ } | ||
static isEvervaultString(str) { | ||
const encryptionSchemeRegex = /^enc:v\d:(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?:(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/; | ||
return encryptionSchemeRegex.test(str); | ||
const splitString = str.split('.'); | ||
if (splitString[0].substr(0,3) === 'eyJ' && splitString[0].substr(0,3) === 'eyJ') { | ||
if (JSON.parse(this.base64UrlDecode(splitString[0])) && JSON.parse(this.base64UrlDecode(splitString[1]))) { | ||
if (JSON.parse(this.base64UrlDecode(splitString[0])).type === 'evervault_string') { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
@@ -139,4 +171,31 @@ | ||
static buildEvervaultObject(keyIv, encryptedData, sharedEncryptedKeys, sharedEncryptedData) { | ||
return { | ||
header: { | ||
type: "evervault_string", | ||
version: "0.1" | ||
}, | ||
body: { | ||
keyIv, | ||
encryptedData, | ||
sharedEncryptedKeys, | ||
sharedEncryptedData | ||
} | ||
}; | ||
} | ||
static buildEvervaultObjectString(evervaultObject) { | ||
const stringifiedHeader = JSON.stringify(evervaultObject.header); | ||
const stringifiedBody = JSON.stringify(evervaultObject.body); | ||
const base64Url = `${this.base64UrlEncode(stringifiedHeader)}.${this.base64UrlEncode(stringifiedBody)}`; | ||
return base64Url; | ||
} | ||
static buildEvervaultString(iv, encoded) { | ||
return `enc:v1:${this.bufferToB64(iv)}:${this.bufferToB64(encoded)}`; | ||
const builtEvervaultObject = this.buildEvervaultObject(this.bufferToB64(iv), this.bufferToB64(encoded)); | ||
const builtEvervaultString = this.buildEvervaultObjectString(builtEvervaultObject); | ||
return builtEvervaultString; | ||
} | ||
@@ -143,0 +202,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69074
1377