298 lines
7.3 KiB
JavaScript
298 lines
7.3 KiB
JavaScript
(function( root ){
|
||
|
||
var DIR = "\\root\\mana"
|
||
var FISECKEY = root.FISECKEY;
|
||
var hDevice = undefined;
|
||
|
||
|
||
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 += "";
|
||
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", 2048, 0);
|
||
|
||
var data = JSON.stringify(info)
|
||
|
||
writeFile(DIR, "name", data)
|
||
|
||
readFile(DIR, "name")
|
||
|
||
}
|
||
|
||
function deleteFile(dir, file){
|
||
if ( ! hDevice ) {
|
||
throw new Error("设备未登录")
|
||
}
|
||
|
||
FISECKEY.FILE_DeleteFile(hDevice, dir, file);
|
||
}
|
||
|
||
function readFile(dir, file) {
|
||
if ( ! hDevice ) {
|
||
throw new Error("设备未登录")
|
||
}
|
||
|
||
var len = FISECKEY.FILE_ReadFile(hDevice, dir, file, 0, 2);
|
||
len = parseInt( len, 10 );
|
||
|
||
var data = FISECKEY.FILE_ReadFile(hDevice, dir, file, 2, len);
|
||
|
||
var sum = sumHash(data);
|
||
var hash = FISECKEY.FILE_ReadFile(hDevice, dir, file, 2 + len, 2);
|
||
if ( sum === hash ) {
|
||
|
||
var str = hexToString(data);
|
||
console.log( "read", data )
|
||
return JSON.parse(str);
|
||
}
|
||
throw new Error("文件读取失败")
|
||
}
|
||
|
||
function writeFile(dir, file, data){
|
||
if ( ! hDevice ) {
|
||
throw new Error("设备未登录")
|
||
}
|
||
|
||
try {
|
||
FISECKEY.FILE_DeleteFile(hDevice, dir, file);
|
||
}catch (e) {
|
||
|
||
}
|
||
|
||
FISECKEY.FILE_CreateFile(hDevice, dir, file, 2048, 0);
|
||
|
||
console.log( "write", data )
|
||
console.log( "data.length" , data.length )
|
||
data = stringToHex( data ).toUpperCase()
|
||
|
||
|
||
var len = data.length / 2
|
||
len = "0000" + len;
|
||
len = len.substr( len.length -4 );
|
||
FISECKEY.FILE_WriteFile(hDevice, dir, file, 0, 2, len);
|
||
FISECKEY.FILE_WriteFile(hDevice, dir, file, 2, data.length/2, data);
|
||
|
||
// console.log( "writeFile=" + len )
|
||
// console.log( "writeFile=" + data )
|
||
// console.log( "writeFile=" + data.length )
|
||
|
||
var hash = sumHash(data);
|
||
// console.log( "writeFile hash=" + hash )
|
||
FISECKEY.FILE_WriteFile(hDevice, dir, file, 2 + (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", 2048, 0);
|
||
writeFile( DIR, "lmk" , data);
|
||
|
||
|
||
var auth = {
|
||
uid : no,
|
||
rid : no
|
||
}
|
||
|
||
FISECKEY.FILE_DeleteFile(hDevice, DIR, "auth");
|
||
FISECKEY.FILE_CreateFile(hDevice, DIR, "auth", 2048, 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", 2048, 0);
|
||
writeFile( DIR, "auth" , auth);
|
||
}
|
||
|
||
function loadAuth(){
|
||
return readFile(DIR, "auth")
|
||
}
|
||
|
||
function catRelease(){
|
||
return readFile(DIR, "name")
|
||
}
|
||
|
||
/**
|
||
* 修改口令
|
||
*/
|
||
function updatePin(oldpin, newpin){
|
||
FISECKEY.USER_ChangePin(hDevice, 1, oldpin, newpin);
|
||
}
|
||
|
||
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
|
||
}
|
||
})( window )
|