[已解決] AU3調用JS 請教
本帖最后由 w60711 于 2019-6-2 13:30 编辑完整解決代碼在7F
-------------------------
请教各位大神小弟第一次接触到JS
很多不懂意思只能猜测..
还请多多指教,感谢~
以下JS要如何调用才有效果呢?
功用是 生成 TOTP 6位数金钥,30秒刷新一次
我的测试1
#include <Date.au3>
Func js()
Local $code
;;TOTP公式 TOTP(K,C)
;;K = Key
;;C = (T - T0) / X
;;T = Unix时间戳
;;T0 = 0 (起始值)
;;X = 30 (30秒刷新一次)
$T = _DateDiff('s', "1970/01/01 08:00:00",_NowCalc())
$X = 30
$K='LFLFMU2SGVCUIUCZKBMEKRKLIQ'
$C = $T / $X
$code = FileRead("totp.js")
$nJS = ObjCreate("MSScriptControl.ScriptControl")
$nJS.language = "JavaScript"
$nJS.addcode($code)
$nReg = $nJS.eval('function HOTP("'&$K&'","'&$C&'")')
Return $nReg
EndFunc ;==>js
MsgBox(0,0,js())
提示:
$nJS.addcode($code)
$nJS^ ERROR
测试2
;以下是使用JavScript代码调用
Func RegCreatePng()
Local $code
$code &= '(function()' & @CRLF
$code &= '{' & @CRLF
$code &= '"use strict";' & @CRLF
$code &= 'function HOTP(K, C)' & @CRLF
$code &= '{' & @CRLF
$code &= 'var key = sjcl.codec.base32.toBits(K);' & @CRLF
$code &= 'var count = [((C & 0xffffffff00000000) >> 32), C & 0xffffffff];' & @CRLF
$code &= 'var otplength = 6;' & @CRLF
$code &= 'var hmacsha1 = new sjcl.misc.hmac(key, sjcl.hash.sha1);' & @CRLF
$code &= 'var code = hmacsha1.encrypt(count);' & @CRLF
$code &= 'var offset = sjcl.bitArray.extract(code, 152, 8) & 0x0f;' & @CRLF
$code &= 'var startBits = offset * 8;' & @CRLF
$code &= 'var endBits = startBits + 4 * 8;' & @CRLF
$code &= 'var slice = sjcl.bitArray.bitSlice(code, startBits, endBits);' & @CRLF
$code &= 'var dbc1 = slice;' & @CRLF
$code &= 'var dbc2 = dbc1 & 0x7fffffff;' & @CRLF
$code &= 'var otp = dbc2 % Math.pow(10, otplength);' & @CRLF
$code &= 'var result = otp.toString();' & @CRLF
$code &= 'while (result.length < otplength)' & @CRLF
$code &= '{' & @CRLF
$code &= "result = '0' + result;" & @CRLF
$code &= '}' & @CRLF
$code &= 'return result;' & @CRLF
$code &= '}' & @CRLF
$code &= 'function GenerateHOTP()' & @CRLF
$code &= '{' & @CRLF
$code &= "var secret = document.getElementById('secret').value;" & @CRLF
$code &= "var counterEl = document.getElementById('hotpcounter');" & @CRLF
$code &= 'var counter = parseInt(counterEl.value, 10);' & @CRLF
$code &= 'var otp = HOTP(secret, counter);' & @CRLF
$code &= "var passwordEl = document.getElementById('hotpresult');" & @CRLF
$code &= 'while (passwordEl.hasChildNodes())' & @CRLF
$code &= '{' & @CRLF
$code &= 'passwordEl.removeChild(passwordEl.firstChild);' & @CRLF
$code &= '}' & @CRLF
$code &= 'passwordEl.textContent = "HOTP: " + otp;' & @CRLF
$code &= 'counterEl.value = counter + 1;' & @CRLF
$code &= '}' & @CRLF
$code &= 'function GenerateTOTP()' & @CRLF
$code &= '{' & @CRLF
$code &= "var secret = document.getElementById('secret').value;" & @CRLF
$code &= 'var ctime = Math.floor((new Date() - 0) / 30000);' & @CRLF
$code &= "var counterEl = document.getElementById('totpcounter');" & @CRLF
$code &= 'counterEl.value = ctime;' & @CRLF
$code &= 'var otp = HOTP(secret, ctime);' & @CRLF
$code &= "var passwordEl = document.getElementById('totpresult');" & @CRLF
$code &= 'while (passwordEl.hasChildNodes())' & @CRLF
$code &= '{' & @CRLF
$code &= 'passwordEl.removeChild(passwordEl.firstChild);' & @CRLF
$code &= '}' & @CRLF
$code &= 'passwordEl.textContent = "TOTP: " + otp;' & @CRLF
$code &= '}' & @CRLF
$code &= 'function ConfigureHandlers()' & @CRLF
$code &= '{' & @CRLF
$code &= "var el = document.getElementById('generateotp');" & @CRLF
$code &= "el.addEventListener('click', GenerateHOTP, false);" & @CRLF
$code &= 'setInterval(GenerateTOTP, 1000);' & @CRLF
$code &= 'GenerateHOTP();' & @CRLF
$code &= 'GenerateTOTP();' & @CRLF
$code &= '}' & @CRLF
$code &= "document.addEventListener('DOMContentLoaded', ConfigureHandlers, false);" & @CRLF
$code &= '}' & @CRLF
$code &= ')();' & @CRLF
$nJS = ObjCreate("ScriptControl")
$nJS.language = "JavaScript"
$nJS.addcode($code)
$nRegCreatePng = $nJS.Run("GenerateTOTP")
Return $nRegCreatePng
EndFunc 提示:
/errorstdout
来源网站:http://www.blogjava.net/baicker/archive/2013/09/11/403712.html
------------- JavaScript 实现 TOTP算法 --------------
sjcl.js
"use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"NOT READY: "+this.message};this.message=a}}};
"undefined"!=typeof module&&module.exports&&(module.exports=sjcl);
sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.g(a.slice(b/32),32-(b&31)).slice(1);return void 0===c?a:sjcl.bitArray.clamp(a,c-b)},extract:function(a,b,c){var d=Math.floor(-b-c&31);return((b+c-1^b)&-32?a<<32-d^a>>>d:a>>>d)&(1<<c)-1},concat:function(a,b){if(0===a.length||0===b.length)return a.concat(b);var c=a,d=sjcl.bitArray.getPartial(c);return 32===d?a.concat(b):sjcl.bitArray.g(b,d,c|0,a.slice(0,a.length-1))},bitLength:function(a){var b=a.length;return 0===
b?0:32*(b-1)+sjcl.bitArray.getPartial(a)},clamp:function(a,b){if(32*a.length<b)return a;a=a.slice(0,Math.ceil(b/32));var c=a.length;b&=31;0<c&&b&&(a=sjcl.bitArray.partial(b,a&2147483648>>b-1,1));return a},partial:function(a,b,c){return 32===a?b:(c?b|0:b<<32-a)+0x10000000000*a},getPartial:function(a){return Math.round(a/0x10000000000)||32},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b))return!1;var c=0,d;for(d=0;d<a.length;d++)c|=a^b;return 0===
c},g:function(a,b,c,d){var e;e=0;for(void 0===d&&(d=[]);32<=b;b-=32)d.push(c),c=0;if(0===b)return d.concat(a);for(e=0;e<a.length;e++)d.push(c|a>>>b),c=a<<32-b;e=a.length?a:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,32<b+a?c:d.pop(),1));return d},j:function(a,b){return^b,a^b,a^b,a^b]}};
sjcl.codec.base32={e:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",fromBits:function(a,b){var c="",d,e=0,g=sjcl.codec.base32.e,f=0,k=sjcl.bitArray.bitLength(a);for(d=0;5*c.length<k;)c+=g.charAt((f^a>>>e)>>>27),5>e?(f=a<<5-e,e+=27,d++):(f<<=5,e-=5);for(;c.length&5&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"").toUpperCase();var b=[],c,d=0,e=sjcl.codec.base32.e,g=0,f;for(c=0;c<a.length;c++){f=e.indexOf(a.charAt(c));if(0>f)throw new sjcl.exception.invalid("this isn't base32!");27<d?(d-=
27,b.push(g^f>>>d),g=f<<32-d):(d+=5,g^=f<<32-d)}d&56&&b.push(sjcl.bitArray.partial(d&56,g,1));return b}};sjcl.hash.sha1=function(a){a?(this.d=a.d.slice(0),this.b=a.b.slice(0),this.a=a.a):this.reset()};sjcl.hash.sha1.hash=function(a){return(new sjcl.hash.sha1).update(a).finalize()};
sjcl.hash.sha1.prototype={blockSize:512,reset:function(){this.d=this.h.slice(0);this.b=[];this.a=0;return this},update:function(a){"string"===typeof a&&(a=sjcl.codec.utf8String.toBits(a));var b,c=this.b=sjcl.bitArray.concat(this.b,a);b=this.a;a=this.a=b+sjcl.bitArray.bitLength(a);for(b=this.blockSize+b&-this.blockSize;b<=a;b+=this.blockSize)n(this,c.splice(0,16));return this},finalize:function(){var a,b=this.b,c=this.d,b=sjcl.bitArray.concat(b,);for(a=b.length+2;a&15;a++)b.push(0);
b.push(Math.floor(this.a/0x100000000));for(b.push(this.a|0);b.length;)n(this,b.splice(0,16));this.reset();return c},h:,i:};
function n(a,b){var c,d,e,g,f,k,m,l=b.slice(0),h=a.d;e=h;g=h;f=h;k=h;m=h;for(c=0;79>=c;c++)16<=c&&(l=(l^l^l^l)<<1|(l^l^l^l)>>>31),d=19>=c?g&f|~g&k:39>=c?g^f^k:59>=c?g&f|g&k|f&k:79>=c?g^f^k:void 0,d=(e<<5|e>>>27)+d+m+l+a.i|0,m=k,k=f,f=g<<30|g>>>2,g=e,e=d;h=h+e|0;h=h+g|0;h=h+f|0;h=h+k|0;h=h+m|0}
sjcl.misc.hmac=function(a,b){this.f=b=b||sjcl.hash.sha256;var c=[[],[]],d,e=b.prototype.blockSize/32;this.c=;a.length>e&&(a=b.hash(a));for(d=0;d<e;d++)c=a^909522486,c=a^1549556828;this.c.update(c);this.c.update(c)};sjcl.misc.hmac.prototype.encrypt=sjcl.misc.hmac.prototype.mac=function(a){a=(new this.f(this.c)).update(a).finalize();return(new this.f(this.c)).update(a).finalize()};totp.js
(function()
{
"use strict";
/*global document, sjcl*/
function HOTP(K, C)
{
var key = sjcl.codec.base32.toBits(K);
// Count is 64 bits long.Note that JavaScript bitwise operations make
// the MSB effectively 0 in this case.
var count = [((C & 0xffffffff00000000) >> 32), C & 0xffffffff];
var otplength = 6;
var hmacsha1 = new sjcl.misc.hmac(key, sjcl.hash.sha1);
var code = hmacsha1.encrypt(count);
var offset = sjcl.bitArray.extract(code, 152, 8) & 0x0f;
var startBits = offset * 8;
var endBits = startBits + 4 * 8;
var slice = sjcl.bitArray.bitSlice(code, startBits, endBits);
var dbc1 = slice;
var dbc2 = dbc1 & 0x7fffffff;
var otp = dbc2 % Math.pow(10, otplength);
var result = otp.toString();
while (result.length < otplength)
{
result = '0' + result;
}
return result;
}
//
// UI Functions
//
function GenerateHOTP()
{
var secret = document.getElementById('secret').value;
var counterEl = document.getElementById('hotpcounter');
var counter = parseInt(counterEl.value, 10);
var otp = HOTP(secret, counter);
var passwordEl = document.getElementById('hotpresult');
while (passwordEl.hasChildNodes())
{
passwordEl.removeChild(passwordEl.firstChild);
}
passwordEl.textContent = "HOTP: " + otp;
counterEl.value = counter + 1;
}
function GenerateTOTP()
{
var secret = document.getElementById('secret').value;
var ctime = Math.floor((new Date() - 0) / 30000);
var counterEl = document.getElementById('totpcounter');
counterEl.value = ctime;
var otp = HOTP(secret, ctime);
var passwordEl = document.getElementById('totpresult');
while (passwordEl.hasChildNodes())
{
passwordEl.removeChild(passwordEl.firstChild);
}
passwordEl.textContent = "TOTP: " + otp;
}
function ConfigureHandlers()
{
var el = document.getElementById('generateotp');
el.addEventListener('click', GenerateHOTP, false);
setInterval(GenerateTOTP, 1000);
GenerateHOTP();
GenerateTOTP();
}
document.addEventListener('DOMContentLoaded', ConfigureHandlers, false);
}
)();index.html
<!DOCTYPE html>
<html manifest="hotp.appcache">
<head>
<meta charset="UTF-8"/>
<title>HOTP/TOTP Demonstration</title>
<script src="sjcl.js"></script>
<script src="totp.js"></script>
<style>
label
{
display: inline-block;
min-width: 12em;
}
input
{
min-width: 30em !important;
}
.hotpresult:before
{
content: "HOTP: ";
}
.totpresult:before
{
content: "TOTP: ";
}
</style>
</head>
<body>
<form id="otpinputs">
<label for="secret">Secret (Base32)</label>
<input id="secret" type="text" value="LFLFMU2SGVCUIUCZKBMEKRKLIQ"/>
<br />
<label for="hotpcounter">HOTP Counter (next value)</label>
<input id="hotpcounter" type="text" value="0"/>
<br />
<label for="totpcounter">TOTP Counter</label>
<input id="totpcounter" type="text" value=""/>
</form>
<button id="generateotp">Generate HOTP</button>
<div id="hotpresult">
</div>
<div id="totpresult">
</div>
</body>
</html>
把js的引号统一试试。不要一些单引号,一些双引号。 对JS脚本不熟悉,对VS脚本略知一二。简单的VS脚本是稍为修改语法可以转换为AU3脚本的。 chishingchan 发表于 2019-5-28 14:52
对JS脚本不熟悉,对VS脚本略知一二。简单的VS脚本是稍为修改语法可以转换为AU3脚本的。
其实只要有能让AU3使用或调用的TOTP都可以...
之前我也有发了一篇老外AU3 TOTP的UDF求救文
可是没有结果XD
(他算出来的OTP和实际上的6位数金钥不符合)
昨天找到这个JS的例子,测试都是正常的
所以才想说看能不能调用,结果还是没搞出个名堂来
还在努力挣扎中...
如果版主愿意出手相助那是最好的了
不才先在此感谢了~ ^^ http://autoitx.com/thread-27656-1-1.html
看看我的贴子是不是能帮到你
x86 调用就行了
my788522 发表于 2019-5-29 11:42
http://autoitx.com/thread-27656-1-1.html
看看我的贴子是不是能帮到你
感谢您
您的代码果真解决了问题^^
虽然JS不会转成AU3语法有点可惜就是了XD
但已经可以成功运作了~
#include <Date.au3>
Dim $code = ''
$code &= '"use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"NOT READY: "+this.message};this.message=a}}};'
$code &= '"undefined"!=typeof module&&module.exports&&(module.exports=sjcl);'
$code &= 'sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.g(a.slice(b/32),32-(b&31)).slice(1);return void 0===c?a:sjcl.bitArray.clamp(a,c-b)},extract:function(a,b,c){var d=Math.floor(-b-c&31);return((b+c-1^b)&-32?a<<32-d^a>>>d:a>>>d)&(1<<c)-1},concat:function(a,b){if(0===a.length||0===b.length)return a.concat(b);var c=a,d=sjcl.bitArray.getPartial(c);return 32===d?a.concat(b):sjcl.bitArray.g(b,d,c|0,a.slice(0,a.length-1))},bitLength:function(a){var b=a.length;return 0==='
$code &= 'b?0:32*(b-1)+sjcl.bitArray.getPartial(a)},clamp:function(a,b){if(32*a.length<b)return a;a=a.slice(0,Math.ceil(b/32));var c=a.length;b&=31;0<c&&b&&(a=sjcl.bitArray.partial(b,a&2147483648>>b-1,1));return a},partial:function(a,b,c){return 32===a?b:(c?b|0:b<<32-a)+0x10000000000*a},getPartial:function(a){return Math.round(a/0x10000000000)||32},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b))return!1;var c=0,d;for(d=0;d<a.length;d++)c|=a^b;return 0==='
$code &= 'c},g:function(a,b,c,d){var e;e=0;for(void 0===d&&(d=[]);32<=b;b-=32)d.push(c),c=0;if(0===b)return d.concat(a);for(e=0;e<a.length;e++)d.push(c|a>>>b),c=a<<32-b;e=a.length?a:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,32<b+a?c:d.pop(),1));return d},j:function(a,b){return^b,a^b,a^b,a^b]}};'
$code &= 'sjcl.codec.base32={e:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",fromBits:function(a,b){var c="",d,e=0,g=sjcl.codec.base32.e,f=0,k=sjcl.bitArray.bitLength(a);for(d=0;5*c.length<k;)c+=g.charAt((f^a>>>e)>>>27),5>e?(f=a<<5-e,e+=27,d++):(f<<=5,e-=5);for(;c.length&5&&!b;)c+="=";return c},toBits:function(a){a=a.replace(/\s|=/g,"").toUpperCase();var b=[],c,d=0,e=sjcl.codec.base32.e,g=0,f;for(c=0;c<a.length;c++){f=e.indexOf(a.charAt(c));if(0>f)throw new sjcl.exception.invalid("this is not base32!");27<d?(d-='
$code &= '27,b.push(g^f>>>d),g=f<<32-d):(d+=5,g^=f<<32-d)}d&56&&b.push(sjcl.bitArray.partial(d&56,g,1));return b}};sjcl.hash.sha1=function(a){a?(this.d=a.d.slice(0),this.b=a.b.slice(0),this.a=a.a):this.reset()};sjcl.hash.sha1.hash=function(a){return(new sjcl.hash.sha1).update(a).finalize()};'
$code &= 'sjcl.hash.sha1.prototype={blockSize:512,reset:function(){this.d=this.h.slice(0);this.b=[];this.a=0;return this},update:function(a){"string"===typeof a&&(a=sjcl.codec.utf8String.toBits(a));var b,c=this.b=sjcl.bitArray.concat(this.b,a);b=this.a;a=this.a=b+sjcl.bitArray.bitLength(a);for(b=this.blockSize+b&-this.blockSize;b<=a;b+=this.blockSize)n(this,c.splice(0,16));return this},finalize:function(){var a,b=this.b,c=this.d,b=sjcl.bitArray.concat(b,);for(a=b.length+2;a&15;a++)b.push(0);'
$code &= 'b.push(Math.floor(this.a/0x100000000));for(b.push(this.a|0);b.length;)n(this,b.splice(0,16));this.reset();return c},h:,i:};'
$code &= 'function n(a,b){var c,d,e,g,f,k,m,l=b.slice(0),h=a.d;e=h;g=h;f=h;k=h;m=h;for(c=0;79>=c;c++)16<=c&&(l=(l^l^l^l)<<1|(l^l^l^l)>>>31),d=19>=c?g&f|~g&k:39>=c?g^f^k:59>=c?g&f|g&k|f&k:79>=c?g^f^k:void 0,d=(e<<5|e>>>27)+d+m+l+a.i|0,m=k,k=f,f=g<<30|g>>>2,g=e,e=d;h=h+e|0;h=h+g|0;h=h+f|0;h=h+k|0;h=h+m|0}'
$code &= 'sjcl.misc.hmac=function(a,b){this.f=b=b||sjcl.hash.sha256;var c=[[],[]],d,e=b.prototype.blockSize/32;this.c=;a.length>e&&(a=b.hash(a));for(d=0;d<e;d++)c=a^909522486,c=a^1549556828;this.c.update(c);this.c.update(c)};sjcl.misc.hmac.prototype.encrypt=sjcl.misc.hmac.prototype.mac=function(a){a=(new this.f(this.c)).update(a).finalize();return(new this.f(this.c)).update(a).finalize()};'
$code &= 'function HOTP(K, C)'
$code &= '{'
$code &= 'var key = sjcl.codec.base32.toBits(K);'
$code &= 'var count = [((C & 0xffffffff00000000) >> 32), C & 0xffffffff];'
$code &= 'var otplength = 6;'
$code &= 'var hmacsha1 = new sjcl.misc.hmac(key, sjcl.hash.sha1);'
$code &= 'var code = hmacsha1.encrypt(count);'
$code &= 'var offset = sjcl.bitArray.extract(code, 152, 8) & 0x0f;'
$code &= 'var startBits = offset * 8;'
$code &= 'var endBits = startBits + 4 * 8;'
$code &= 'var slice = sjcl.bitArray.bitSlice(code, startBits, endBits);'
$code &= 'var dbc1 = slice;'
$code &= 'var dbc2 = dbc1 & 0x7fffffff;'
$code &= 'var otp = dbc2 % Math.pow(10, otplength);'
$code &= 'var result = otp.toString();'
$code &= 'while (result.length < otplength)'
$code &= '{'
$code &= "result = '0' + result;"
$code &= '}'
$code &= 'return result;'
$code &= '}'
Dim $K = 'LFLFMU2SGVCUIUCZKBMEKRKLIQ'
Dim $unixTime = _GetUnixTimeUTC()
Dim $counter = Int($unixTime) / 30
Dim $C = Floor($counter)
$nJS = ObjCreate("MSScriptControl.ScriptControl")
$nJS.language = "JavaScript"
$nJS.addcode($code)
$nRegCreatePng = $nJS.eval('HOTP("' & $K & '","' & $C & '")')
MsgBox(0,0,$nRegCreatePng)
Func _GetUnixTimeUTC()
; returns number of seconds since EPOCH in UTC
Local $aSysTimeInfo = _Date_Time_GetTimeZoneInformation()
Local $utcTime = ""
Local $sDate = _NowCalc()
If $aSysTimeInfo = 2 Then
$utcTime = _DateAdd('n', $aSysTimeInfo + $aSysTimeInfo, $sDate)
Else
$utcTime = _DateAdd('n', $aSysTimeInfo, $sDate)
EndIf
Return _DateDiff('s', "1970/01/01 00:00:00", $utcTime)
EndFunc
页:
[1]