(function( root ){ var DIR = "\\root\\mana" var FISECKEY = root.FISECKEY; var hDevice = undefined; var fileSize = 4 * 1024; if ( ! FISECKEY ) { console.error("缺少 FISECKEY 支持 !!!") } function stringToHex(str){ var val = ""; for (var i = 0; i < str.length; i++) { var v = str.charCodeAt(i).toString(16); if ( v.length < 2 ) { v = "0" + v; } val += v; } 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 += ""; if ( sum.length < 4 ) { sum = "0000" + sum } // 截取最后 2 字节 return sum.substr( sum.length -4 ); } function release( info ){ if ( ! hDevice ) { throw new Error("设备未登录") } FISECKEY.FILE_Init(hDevice); FISECKEY.FILE_CreateDir(hDevice, DIR, 0); FISECKEY.FILE_CreateFile(hDevice, DIR, "name", 1024, 0); FISECKEY.GenECCKeypair(hDevice, 0, 256, 1); var data = JSON.stringify(info) writeFile(DIR, "name", data, 1024) readFile(DIR, "name") } function deleteFile(dir, file){ if ( ! hDevice ) { throw new Error("设备未登录") } FISECKEY.FILE_DeleteFile(hDevice, dir, file); } 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; } } function readFile(dir, file) { if ( ! hDevice ) { throw new Error("设备未登录") } var len = FISECKEY.FILE_ReadFile(hDevice, dir, file, 0, 3); console.log("read "+ file + " lenAsHex=", len, ) len = parseInt( len, 10 ); console.log("read "+ file + " len=", len) var data = readData(hDevice, dir, file, 3, len); console.log("read "+ file + " data=", data) var sum = sumHash(data); var hash = FISECKEY.FILE_ReadFile(hDevice, dir, file, 3 + len, 2); if ( sum === hash ) { var str = hexToString(data); console.log( "read", data ) return JSON.parse(str); } throw new Error("文件读取失败") } 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) ) } } function writeFile(dir, file, data, keep, size){ if ( ! hDevice ) { throw new Error("设备未登录") } if ( ! keep ) { if ( !size ) { size = fileSize } try { FISECKEY.FILE_DeleteFile(hDevice, dir, file); }catch (e) { } FISECKEY.FILE_CreateFile(hDevice, dir, file, size, 0); } console.log( "write "+ file + " data=", data ) console.log( "write "+ file + " data.length" , data.length ) data = stringToHex( data ).toUpperCase() var len = data.length / 2 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) // console.log( "writeFile=" + len ) // console.log( "writeFile=" + data ) // console.log( "writeFile=" + data.length ) var hash = sumHash(data); console.log("write "+ file + " hash=", hash) // console.log( "writeFile hash=" + hash ) FISECKEY.FILE_WriteFile(hDevice, dir, file, 3 + (data.length/2), 2, hash); } // 枚举序列号与名称 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 HEX(48 字节) */ 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"); FISECKEY.FILE_CreateFile(hDevice, DIR, "lmk", fileSize, 0); writeFile( DIR, "lmk" , data); var auth = { uid : no, rid : no } FISECKEY.FILE_DeleteFile(hDevice, DIR, "auth"); FISECKEY.FILE_CreateFile(hDevice, DIR, "auth", fileSize, 0); writeFile( DIR, "auth" , auth); } function saveAuth(uid, rid){ var auth = { uid : no, rid : no } FISECKEY.FILE_DeleteFile(hDevice, DIR, "auth"); FISECKEY.FILE_CreateFile(hDevice, DIR, "auth", fileSize, 0); writeFile( DIR, "auth" , auth); } function loadAuth(){ return readFile(DIR, "auth") } function catRelease(){ return readFile(DIR, "name") } /** * 修改口令 */ function updatePin(flag, oldpin, newpin){ FISECKEY.USER_ChangePin(hDevice, 3, oldpin, newpin); } function exportPk(){ return FISECKEY.ExportECCKeypair(hDevice, 1); } function sign(vInbuf){ return FISECKEY.ECCSign(hDevice, 1, 1, vInbuf); } root.UKey = { release: release, catRelease : catRelease, readFile: readFile, writeFile : writeFile, deleteFile : deleteFile, login : login, logout : logout, updatePin : updatePin, admin : admin, enumDevByArray : enumDevByArray, enumDir: enumDir, saveLMK : saveLMK, loadLMK : loadLMK, saveAuth : saveAuth, loadAuth : loadAuth, exportPk : exportPk, sign : sign } })( window )