philomena/assets/js/fingerprint.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-10-05 02:09:52 +02:00
/**
* Fingerprints
*/
// http://stackoverflow.com/a/34842797
function hashCode(str) {
return str.split('').reduce((prevHash, currVal) =>
((prevHash << 5) - prevHash) + currVal.charCodeAt(0), 0) >>> 0;
}
function createFingerprint() {
const prints = [
navigator.userAgent,
navigator.cpuClass,
navigator.oscpu,
navigator.platform,
navigator.browserLanguage,
navigator.language,
navigator.systemLanguage,
navigator.userLanguage,
screen.availLeft,
screen.availTop,
screen.availWidth,
screen.height,
screen.width,
window.devicePixelRatio,
new Date().getTimezoneOffset(),
];
return hashCode(prints.join(''));
}
function setFingerprintCookie() {
let fingerprint;
// The prepended 'c' acts as a crude versioning mechanism.
try {
fingerprint = `c${createFingerprint()}`;
}
// If fingerprinting fails, use fakeprint "c1836832948" as a last resort.
catch (err) {
fingerprint = 'c1836832948';
}
document.cookie = `_ses=${fingerprint}; path=/; SameSite=Lax`;
2019-10-05 02:09:52 +02:00
}
export { setFingerprintCookie };