browser-canvas-fingerprinting
Version:
A simple canvas fingerprinting implementation in browser with specific information used to generate fingerprint
44 lines (33 loc) • 1.06 kB
JavaScript
export function keyCount(obj) {
if (!obj) return 0;
const visited = new WeakSet();
function countKeys(currentObj) {
if (currentObj === null || typeof currentObj !== 'object') {
return 0;
}
if (visited.has(currentObj)) {
return 0;
}
visited.add(currentObj);
let count = Reflect.ownKeys(currentObj).length;
const proto = Object.getPrototypeOf(currentObj);
if (proto !== null) {
count += countKeys(proto);
}
return count;
}
return countKeys(obj);
}
export function urand(bits) {
if (bits <= 0 || !Number.isInteger(bits)) {
throw new Error('bits must be a positive integer');
}
const bytesNeeded = Math.ceil(bits / 8);
const byteArray = new Uint8Array(bytesNeeded);
crypto.getRandomValues(byteArray);
let result = 0n;
for (let i = 0; i < bytesNeeded; i++) {
result = (result << 8n) | BigInt(byteArray[i]);
}
return result & ((1n << BigInt(bits)) - 1n);
}