Changed hashing algorithm.

pull/186/head
Mikunj 6 years ago
parent a989a1a818
commit d30e950bf9

@ -1,6 +1,7 @@
/* global
Signal,
textsecure,
StringView
*/
/* eslint-disable no-bitwise */
@ -61,17 +62,10 @@
};
}
function hashCode(string) {
let hash = 0;
if (string.length === 0) {
return hash;
}
for (let i = 0; i < string.length; i += 1) {
const char = string.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash &= hash; // Convert to 32bit integer
}
return hash;
async function sha256(string) {
const arraybuffer = new TextEncoder('utf-8').encode(string);
const digest = await window.crypto.subtle.digest('SHA-256', arraybuffer);
return StringView.arrayBufferToHex(digest);
}
async function getPreview(url) {
@ -132,11 +126,13 @@
}
}
const hash = await sha256(url);
return {
title,
url,
image,
hash: hashCode(url),
hash,
};
}

@ -159,10 +159,11 @@
const { data } = image;
const extension = Attachment.getFileExtension(image);
if (data && extension) {
const hash32 = hash.substring(0, 32);
try {
const filePath = await writeAttachment({
data,
path: `previews/${hash}.${extension}`,
path: `previews/${hash32}.${extension}`,
});
// return the image without the data

Loading…
Cancel
Save