Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getNameOfViewerResource(logdir: string): string {
// TODO: find some hash function with shorter resulting message.
return 'viewer-' + crypto.SHA1(logdir);
}
}
const message = Object.assign({}, messageObj)
// set timestamp
message.created = Date.now()
if (!validateMessage(message)) {
debug('ERR: invalid message')
return
}
for (const key of convObj.keys) {
try {
const iv = CryptoJS.lib.WordArray.random(16)
const messageStr = JSON.stringify(message)
const shaSub = CryptoJS.enc.Base64.stringify(
CryptoJS.SHA1(messageStr)
).substr(0, 6)
const encmsg = CryptoJS.AES.encrypt(messageStr + shaSub, key, {
iv: iv
}).toString()
const ivStr = CryptoJS.enc.Base64.stringify(iv)
return {
type: 'msg',
emsg: encmsg,
i: ivStr,
address: this.account_key
}
} catch (err) {
debug('Encryption failed', err)
// Continue to try and encrypt with other keys
}
const buffer = CryptoJS.AES.decrypt(msg, key, {
iv: CryptoJS.enc.Base64.parse(ivStr)
})
let outText
try {
outText = buffer.toString(CryptoJS.enc.Utf8)
} catch (error) {
return
}
if (outText && outText.length > 6) {
const verifyText = outText.slice(0, -6)
const shaCheck = outText.substr(-6)
if (
shaCheck ==
CryptoJS.enc.Base64.stringify(CryptoJS.SHA1(verifyText)).substr(0, 6)
) {
return verifyText
}
}
}
const buffer = CryptoJS.AES.decrypt(msg, key, {
iv: CryptoJS.enc.Base64.parse(iv_str)
})
let out_text
try {
out_text = buffer.toString(CryptoJS.enc.Utf8)
} catch (error) {
return
}
if (out_text && out_text.length > 6) {
const verify_text = out_text.slice(0, -6)
const sha_check = out_text.substr(-6)
if (
sha_check ==
CryptoJS.enc.Base64.stringify(CryptoJS.SHA1(verify_text)).substr(0, 6)
) {
return verify_text
}
}
}
const buffer = CryptoJS.AES.decrypt(msg, key, {
iv: CryptoJS.enc.Base64.parse(ivStr)
})
let outText
try {
outText = buffer.toString(CryptoJS.enc.Utf8)
} catch (error) {
return
}
if (outText && outText.length > 6) {
const verifyText = outText.slice(0, -6)
const shaCheck = outText.substr(-6)
if (
shaCheck ==
CryptoJS.enc.Base64.stringify(CryptoJS.SHA1(verifyText)).substr(0, 6)
) {
return verifyText
}
}
}
private _generateHash(): string {
if (_.isEmpty(this.content)) {
throw new PlaygroundError('Snippet hash failed. Cannot create hash of null or undefined');
}
return crypto.SHA1(JSON.stringify(this.content)).toString();
}
}
CryptoAdapter.SHA1 = function SHA1(message) {
return CryptoJS.SHA1(message).toString();
};
function createNoficication(type: SystemNotificationType, title: string, message: string) {
const id = SHA1(type + title + message).toString().toLowerCase();
const docRef = siteCollection().collection('notifications').doc(id);
return admin.firestore().runTransaction(async (trx) => {
const trxNotifDoc = await trx.get(docRef);
if (trxNotifDoc.exists) {
trx.update(docRef, {
updated: admin.firestore.FieldValue.serverTimestamp(),
numOccurances: admin.firestore.FieldValue.increment(1),
isRead: false,
} as SystemNotification);
} else {
trx.set(docRef, {
id: id,
type: 'error',
title: title,
message: message,
toSHA1(text) {
if (CryptoJS && CryptoJS.SHA1)
return CryptoJS.SHA1(text).toString();
else
return Crypto.util.bytesToHex(Crypto.SHA1(text, { asBytes: true }));
}