Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function toDataUrl(arrayBuffer) {
return 'data:image/png;base64,' + base64.encode(arrayBuffer);
}
async sendSeed(requesterPublicKey) {
const requesterPublicKeyArrayBuffer = new Uint8Array(base64.decode(requesterPublicKey))
const requesterDeviceId = base64.encode(await crypto.sha256.hash(requesterPublicKeyArrayBuffer))
if (this.sentSeedTo[requesterDeviceId] || this.processingSeedRequest[requesterDeviceId]) return
this.processingSeedRequest[requesterDeviceId] = true
if (window.confirm(`Send the secret key to device with Device ID: \n\n${requesterDeviceId}\n`)) {
try {
const sharedKey = await crypto.diffieHellman.getSharedKey(
this.keys.dhPrivateKey,
requesterPublicKeyArrayBuffer
)
const encryptedSeed = await crypto.aesGcm.encryptString(sharedKey, this.seedString)
const action = 'SendSeed'
const params = { requesterPublicKey, encryptedSeed }
const getKeyStringFromKey = async (key) => {
const rawKey = await window.crypto.subtle.exportKey(RAW_KEY_TYPE, key)
const keyString = base64.encode(rawKey)
return keyString
}
const masterKey = await crypto.hkdf.importMasterKey(seed)
const encryptionKeySalt = crypto.hkdf.generateSalt()
const dhKeySalt = crypto.hkdf.generateSalt()
const hmacKeySalt = crypto.hkdf.generateSalt()
const dhPrivateKey = await crypto.diffieHellman.importKeyFromMaster(masterKey, dhKeySalt)
const publicKey = crypto.diffieHellman.getPublicKey(dhPrivateKey)
try {
const session = await api.auth.signUp(
username,
passwordSecureHash,
base64.encode(publicKey),
base64.encode(encryptionKeySalt),
base64.encode(dhKeySalt),
base64.encode(hmacKeySalt),
email,
profile,
pbkdfKeySalt && base64.encode(pbkdfKeySalt),
passwordEncryptedSeed && base64.encode(passwordEncryptedSeed)
)
return session
} catch (e) {
_parseUserResponseError(e, username)
}
}
toJSON() {
return {
...super._toJSON(),
data: encodeBase64(this.content),
};
}
toFullJSON() {
return {
...super.toFullJSON(),
bin: true,
data: encodeBase64(this.content),
};
}
}
async validateKey() {
const sharedKey = await crypto.diffieHellman.getSharedKeyWithServer(this.keys.dhPrivateKey)
const validationMessage = base64.encode(await crypto.aesGcm.decrypt(sharedKey, this.encryptedValidationMessage))
const action = 'ValidateKey'
const params = { validationMessage }
await this.request(action, params)
}
function prepData(data) {
if (data === null || data === undefined || typeof (data) !== 'object')
return data;
if (data.constructor == ArrayBuffer || data.constructor == Uint8Array) {
return randomString + Base64.encode(data);
} else if (data.constructor == Array) {
var newArray = new Array(data.length);
for (var i = 0; i < data.length; i++) {
newArray[i] = prepData(data[i]);
}
return newArray;
} else {
var newObject = {};
for (var prop in data) {
newObject[prop] = prepData(data[prop]);
}
return newObject;
}
}
async setFileContent(fileId: number, file: ArrayBuffer) {
await this._initialize()
let byteLengthPreCompression = file.byteLength
if (!JSZip.support.arraybuffer)
throw new Error('Your browser does not support JSZip. Please install a compatible browser.')
let zip = JSZip()
zip.file('content', file)
file = await zip.generateAsync({type : 'arraybuffer'})
const contentStorageRef = await this._storage.addFile(file)
let fileInfos = await this.getFileInfo(fileId)
let jsonFileData = JSON.parse(Buffer.from(abBase64.encode(await this._storage.getFile(fileInfos.storageRef)), 'base64').toString('ascii'))
jsonFileData.contentStorageRef = contentStorageRef
jsonFileData.lastModification = new Date()
jsonFileData.fileSize = byteLengthPreCompression
const fileDataStorageRef = await this._storage.addFile(abBase64.decode(Buffer.from(JSON.stringify(jsonFileData)).toString('base64')))
await this._contract.setStorageRef(fileId, fileDataStorageRef)
this._sendEvent('FileChange', { fileId });
}
const masterKey = await crypto.hkdf.importMasterKey(seed)
const encryptionKeySalt = crypto.hkdf.generateSalt()
const dhKeySalt = crypto.hkdf.generateSalt()
const hmacKeySalt = crypto.hkdf.generateSalt()
const dhPrivateKey = await crypto.diffieHellman.importKeyFromMaster(masterKey, dhKeySalt)
const publicKey = crypto.diffieHellman.getPublicKey(dhPrivateKey)
try {
const session = await api.auth.signUp(
username,
passwordSecureHash,
base64.encode(publicKey),
base64.encode(encryptionKeySalt),
base64.encode(dhKeySalt),
base64.encode(hmacKeySalt),
email,
profile,
pbkdfKeySalt && base64.encode(pbkdfKeySalt),
passwordEncryptedSeed && base64.encode(passwordEncryptedSeed)
)
return session
} catch (e) {
_parseUserResponseError(e, username)
}
}