fullstack.web/swa/u/ukey.js

369 lines
9.2 KiB
JavaScript
Raw Permalink Normal View History

2022-12-22 06:57:51 +00:00
(function( root ){
2023-01-04 02:30:35 +00:00
var DIR = "\\root\\mana"
2022-12-22 06:57:51 +00:00
var FISECKEY = root.FISECKEY;
var hDevice = undefined;
2024-09-19 07:25:56 +00:00
var fileSize = 4 * 1024;
2022-12-22 06:57:51 +00:00
if ( ! FISECKEY ) {
console.error("缺少 FISECKEY 支持 !!!")
}
function stringToHex(str){
var val = "";
for (var i = 0; i < str.length; i++) {
2023-01-04 02:30:35 +00:00
var v = str.charCodeAt(i).toString(16);
if ( v.length < 2 ) {
v = "0" + v;
}
val += v;
2022-12-22 06:57:51 +00:00
}
return val
}
function hexToString(hex){
var arr = hex.split("")
var out = ""
for (var i = 0; i < arr.length / 2; i++) {
var tmp = "0x" + arr[i * 2] + arr[i * 2 + 1]
var charValue = String.fromCharCode(tmp);
out += charValue
}
return out
}
function admin( dev, pin ){
if ( ! hDevice ) {
hDevice = FISECKEY.OpenBySerial(dev, "4");
}
if ( ! hDevice ) {
throw new Error("设备打开失败")
}
FISECKEY.ADMIN_Login(hDevice, pin);
return hDevice;
}
function login( dev, pin ){
hDevice = FISECKEY.OpenBySerial(dev, "4");
if ( ! hDevice ) {
throw new Error("设备打开失败")
}
FISECKEY.USER_Login(hDevice, pin);
return hDevice;
}
function logout(){
FISECKEY.USER_Logout(hDevice, 0);
FISECKEY.CloseDevice(hDevice);
hDevice = undefined
}
function sumHash(data){
var sum = 0;
for (var i =0 ; i < data.length; i++) {
sum += data.charCodeAt(i)
}
sum += "";
2024-09-19 07:25:56 +00:00
if ( sum.length < 4 ) {
sum = "0000" + sum
}
// 截取最后 2 字节
2022-12-22 06:57:51 +00:00
return sum.substr( sum.length -4 );
}
function release( info ){
if ( ! hDevice ) {
throw new Error("设备未登录")
}
FISECKEY.FILE_Init(hDevice);
2024-09-19 07:25:56 +00:00
2022-12-22 06:57:51 +00:00
FISECKEY.FILE_CreateDir(hDevice, DIR, 0);
2024-09-19 07:25:56 +00:00
FISECKEY.FILE_CreateFile(hDevice, DIR, "name", 1024, 0);
2022-12-22 06:57:51 +00:00
2023-08-09 05:57:49 +00:00
FISECKEY.GenECCKeypair(hDevice, 0, 256, 1);
2022-12-22 06:57:51 +00:00
var data = JSON.stringify(info)
2024-09-19 07:25:56 +00:00
writeFile(DIR, "name", data, 1024)
2022-12-22 06:57:51 +00:00
readFile(DIR, "name")
}
2023-01-04 02:30:35 +00:00
function deleteFile(dir, file){
if ( ! hDevice ) {
throw new Error("设备未登录")
}
FISECKEY.FILE_DeleteFile(hDevice, dir, file);
}
2024-09-19 07:25:56 +00:00
function readData(hDevice, dir, file, off, len){
var data = "";
if ( len <= 1024 ) {
return FISECKEY.FILE_ReadFile(hDevice, dir, file, off, len);
}
else {
var curLen = 0;
while ( curLen < len ) {
var readLen = 1024;
if ( (len - curLen) <= 1024 ) {
// 剩余量小于= 1024
readLen = len - curLen;
} else {
// 剩余量大于 1024
readLen = 1024
}
data += FISECKEY.FILE_ReadFile(hDevice, dir, file, off + curLen, readLen);
curLen += readLen
}
return data;
}
}
2022-12-22 06:57:51 +00:00
function readFile(dir, file) {
if ( ! hDevice ) {
throw new Error("设备未登录")
}
2024-09-19 07:25:56 +00:00
var len = FISECKEY.FILE_ReadFile(hDevice, dir, file, 0, 3);
console.log("read "+ file + " lenAsHex=", len, )
2022-12-22 06:57:51 +00:00
len = parseInt( len, 10 );
2024-09-19 07:25:56 +00:00
console.log("read "+ file + " len=", len)
2022-12-22 06:57:51 +00:00
2024-09-19 07:25:56 +00:00
var data = readData(hDevice, dir, file, 3, len);
console.log("read "+ file + " data=", data)
2022-12-22 06:57:51 +00:00
var sum = sumHash(data);
2024-09-19 07:25:56 +00:00
var hash = FISECKEY.FILE_ReadFile(hDevice, dir, file, 3 + len, 2);
2022-12-22 06:57:51 +00:00
if ( sum === hash ) {
2023-01-04 02:30:35 +00:00
var str = hexToString(data);
console.log( "read", data )
return JSON.parse(str);
2022-12-22 06:57:51 +00:00
}
throw new Error("文件读取失败")
}
2023-01-10 02:20:52 +00:00
function writeData(hDevice, dir, file, idx, len, data){
if ( len <= 400 ) {
FISECKEY.FILE_WriteFile(hDevice, dir, file, idx, len, data);
} else {
// 先写入 400
FISECKEY.FILE_WriteFile(hDevice, dir, file, idx, 400, data.substr(0, 800 ));
writeData( hDevice, dir, file, idx + 400, len - 400, data.substr(800) )
}
}
2024-09-19 07:25:56 +00:00
function writeFile(dir, file, data, keep, size){
2022-12-22 06:57:51 +00:00
if ( ! hDevice ) {
throw new Error("设备未登录")
}
2024-09-19 07:25:56 +00:00
if ( ! keep ) {
if ( !size ) {
size = fileSize
}
try {
FISECKEY.FILE_DeleteFile(hDevice, dir, file);
}catch (e) {
2023-01-04 02:30:35 +00:00
2024-09-19 07:25:56 +00:00
}
FISECKEY.FILE_CreateFile(hDevice, dir, file, size, 0);
2023-01-04 02:30:35 +00:00
}
2024-09-19 07:25:56 +00:00
console.log( "write "+ file + " data=", data )
console.log( "write "+ file + " data.length" , data.length )
2022-12-22 06:57:51 +00:00
data = stringToHex( data ).toUpperCase()
2023-01-04 02:30:35 +00:00
2022-12-22 06:57:51 +00:00
var len = data.length / 2
2024-09-19 07:25:56 +00:00
len = "000000" + len;
len = len.substr( len.length -6 );
console.log("write "+ file + " head=", len)
FISECKEY.FILE_WriteFile(hDevice, dir, file, 0, 3, len);
writeData(hDevice, dir, file, 3, data.length/2, data)
2022-12-22 06:57:51 +00:00
// console.log( "writeFile=" + len )
// console.log( "writeFile=" + data )
// console.log( "writeFile=" + data.length )
2024-09-19 07:25:56 +00:00
2022-12-22 06:57:51 +00:00
var hash = sumHash(data);
2024-09-19 07:25:56 +00:00
console.log("write "+ file + " hash=", hash)
2022-12-22 06:57:51 +00:00
// console.log( "writeFile hash=" + hash )
2024-09-19 07:25:56 +00:00
FISECKEY.FILE_WriteFile(hDevice, dir, file, 3 + (data.length/2), 2, hash);
2022-12-22 06:57:51 +00:00
}
// 枚举序列号与名称
function enumDevByArray() {
return FISECKEY.EnumByArray();
}
function enumDir( path ){
if ( ! hDevice ) {
throw new Error("设备未登录")
}
var folders = FISECKEY.FILE_EnmuDir(hDevice, path);
var fos = folders.split("|")
folders = []
for ( var i = 0 ; i < fos.length ; i++ ) {
if ( fos[i] ) {
folders.push( path + "\\" + fos[i] );
}
}
var files = FISECKEY.FILE_EnmuFile(hDevice, path);
var fs = files.split("|")
files = []
for ( var i = 0 ; i < fs.length ; i++ ) {
if ( fs[i] ) {
files.push( path + "\\" + fs[i] );
}
}
return {
folders : folders,
files : files
}
}
function loadLMK(){
return readFile(DIR, "lmk")
}
/**
*
* @param no 1/2/3
* @param mkey HEX48 字节
*/
function saveLMK(no, lmk, pk, sk) {
if ( ! hDevice ) {
throw new Error("设备未登录")
}
var info = {
pk : pk , sk : sk
}
switch ( no ) {
case 1: {
info = Object.assign( info, {
no : no,
lmk1 : lmk.substr(0, 32),
lmk2 : lmk.substr(32, 64)
})
break;
}
case 2: {
// 0 16 32 48
// 0 32 64 96
info = Object.assign( info, {
no : no,
lmk2 : lmk.substr(32, 64),
lmk3 : lmk.substr(64, 96)
})
break;
}
case 3: {
info = Object.assign( info, {
no : no,
lmk3 : lmk.substr(64, 96),
lmk1 : lmk.substr(0, 32)
})
break;
}
default :
throw new Error("不支持的分量")
}
var data = JSON.stringify(info)
FISECKEY.FILE_DeleteFile(hDevice, DIR, "lmk");
2023-01-10 02:20:52 +00:00
FISECKEY.FILE_CreateFile(hDevice, DIR, "lmk", fileSize, 0);
2022-12-22 06:57:51 +00:00
writeFile( DIR, "lmk" , data);
var auth = {
uid : no,
rid : no
}
FISECKEY.FILE_DeleteFile(hDevice, DIR, "auth");
2023-01-10 02:20:52 +00:00
FISECKEY.FILE_CreateFile(hDevice, DIR, "auth", fileSize, 0);
2022-12-22 06:57:51 +00:00
writeFile( DIR, "auth" , auth);
}
function saveAuth(uid, rid){
var auth = {
uid : no,
rid : no
}
FISECKEY.FILE_DeleteFile(hDevice, DIR, "auth");
2023-01-10 02:20:52 +00:00
FISECKEY.FILE_CreateFile(hDevice, DIR, "auth", fileSize, 0);
2022-12-22 06:57:51 +00:00
writeFile( DIR, "auth" , auth);
}
function loadAuth(){
return readFile(DIR, "auth")
}
function catRelease(){
return readFile(DIR, "name")
}
/**
* 修改口令
*/
2024-09-19 07:25:56 +00:00
function updatePin(flag, oldpin, newpin){
FISECKEY.USER_ChangePin(hDevice, 3, oldpin, newpin);
2022-12-22 06:57:51 +00:00
}
2023-08-09 05:57:49 +00:00
function exportPk(){
return FISECKEY.ExportECCKeypair(hDevice, 1);
}
function sign(vInbuf){
return FISECKEY.ECCSign(hDevice, 1, 1, vInbuf);
}
2022-12-22 06:57:51 +00:00
root.UKey = {
release: release,
catRelease : catRelease,
readFile: readFile,
writeFile : writeFile,
2023-01-04 02:30:35 +00:00
deleteFile : deleteFile,
2022-12-22 06:57:51 +00:00
login : login,
logout : logout,
updatePin : updatePin,
admin : admin,
enumDevByArray : enumDevByArray,
enumDir: enumDir,
saveLMK : saveLMK,
loadLMK : loadLMK,
saveAuth : saveAuth,
2023-08-09 05:57:49 +00:00
loadAuth : loadAuth,
exportPk : exportPk,
sign : sign
2022-12-22 06:57:51 +00:00
}
})( window )