需要移入crypto-js-4.0.0.min.js
官方文档:https://cryptojs.gitbook.io/docs/
github:https://github.com/brix/crypto-js
finger: function () {
// 获取浏览器 User Agent 信息
var userAgent = navigator.userAgent;
// 获取浏览器 Accept Headers 信息
var acceptHeaders = navigator.acceptHeader;
// 获取屏幕分辨率
var screenWidth = screen.width;
var screenHeight = screen.height;
// 获取时区偏移量
var timezoneOffset = new Date().getTimezoneOffset();
// 创建一个 Canvas 元素
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// 绘制一个图像,并获取图像数据的哈希值
var text = 'fingerprint';
ctx.textBaseline = "top";
ctx.font = "14px 'Arial'";
ctx.textBaseline = "alphabetic";
ctx.fillStyle = "#f60";
ctx.fillRect(125, 1, 62, 20);
ctx.fillStyle = "#069";
ctx.fillText(text, 2, 15);
ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
ctx.fillText(text, 4, 17);
var canvasData = canvas.toDataURL();
var canvasHash = hash(canvasData);
// 获取本地 IP 地址和端口号
var rtcPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
if (rtcPeerConnection) {
var pc = new rtcPeerConnection({
iceServers: []
});
pc.createDataChannel("");
pc.createOffer(function (result) {
pc.setLocalDescription(result, function () { }, function () { });
}, function () { });
pc.onicecandidate = function (event) {
if (event && event.candidate && event.candidate.candidate) {
var address = event.candidate.candidate.split(' ')[4];
var port = event.candidate.candidate.split(' ')[5];
var ip = address.split(':')[0];
}
};
}
// 整合上述信息生成浏览器指纹
var fingerprint = userAgent + acceptHeaders + screenWidth + screenHeight + timezoneOffset + canvasHash;
// 计算哈希值
function hash(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
var char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return hash;
}
return CryptoJS.MD5(fingerprint).toString();
}