Changed hashing algorithm.

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

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

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

Loading…
Cancel
Save