把开源的一个两步验证写成aardio格式了,抛砖引玉,希望aardio生态越来越好。
//otp 动态令牌两步验证支持库
namespace kio;
import com;
import dotNet;
import System;
//参考:https://githubfast.com/kspearrin/Otp.NET
namespace otp {
dll = ..dotNet.loadFile($ "~\lib\kio\otp\Otp.NET.dll");
base = dll.import("OtpNet");
Base32 = function() {
return base.Base32Encoding();
}
Hotp = function(secretKey, mode = 0, totpSize = 6) {
return base.Hotp(Base32().ToBytes(secretKey), mode, totpSize);
}
Totp = function(secretKey, step = 30, mode = 0, totpSize = 6) {
return base.Totp(Base32().ToBytes(secretKey), step, mode, totpSize);
}
VerificationWindow = function(previous = 0, future = 0) {
return base.VerificationWindow(previous, future);
}
OtpUri=function(secrt,user,issuer,otptype=0,mode=0,digits=6,period=30,couner=0){
return base.OtpUri(otptype,secrt,user,issuer,mode,digits,period,couner);
}
GenerateRandomKey=function(mode = 0){
select(mode) {
case 2 {
mode=64;
}
case 1 {
mode=32;
}
else {
mode=20;
}
}
return base.Base32Encoding().ToString(base.KeyGeneration.GenerateRandomKey(mode));
}
}
/**intellisense(kio)
otp = otp动态令牌操作库(两步验证)
end intellisense**/
/**intellisense(kio.otp)
Base32()=创建Base32\n!Base32.
GenerateRandomKey(__)=创建随机密钥,默认Sha1模式\n参数: 0 Sha1 \n 1 Sha256 \n 2 Sha512
Hotp("__")=创建Hotp,事件同步一次性密码\n!Hotp.
Hotp("__",mode,totpSize)=创建Hotp,事件同步一次性密码\n参数1:字符串,Base32格式secret\n参数2:模式,Sha1 0 Sha256 1 Sha512 2\n参数3:验证码长度,常见6或8\n!Hotp.
Totp("__")=创建Totp,时间同步一次性密码\n参数:字符串,Base32格式secret,默认30s刷新6位长度Sha1\n!Totp.
Totp("__",step,mode,totpSize)=创建Totp,时间同步一次性密码\n参数1:字符串,Base32格式secret\n参数2:刷新时间,15或30或60\n参数3:模式,Sha1 0 Sha256 1 Sha512\n参数4:验证码长度,常见6或8\n!Totp.
VerificationWindow()=创建VerificationWindow\n参数1:整数,Base32格式secret\n参数2:整数\n!VerificationWindow.
OtpUri("__","user","isuser",0/*Totp*/,0/*sha1*/,6,30,0/*counter*/)=创建OtpUri\n!OtpUri.
OtpUri("__","user")=创建OtpUri,默认totp,sha1,6,30\n!OtpUri.
Totp()=!Totp.
Hotp()=!Hotp.
OtpUri()=!OtpUri.
end intellisense**/
/**intellisense(!OtpUri)
ToString()=返回字符串
end intellisense**/
/**intellisense(!Totp)
ComputeTotp()=验证码
RemainingSeconds()=剩余秒数
VerifyTotp=@//基于RFC建议验证,通过返回true\n//参数1:字符串验证码\n//参数2:C# out timeWindowUsed的长输出\n//参数3:RFC建议允许延迟一个步长,如创建时步长15则验证时间为30,以此类推\n\nvar rfc=kio.otp.VerificationWindow(1,1)\nvar timeWindowUsed=com.long(0)\nvar t=__.VerifyTotp("000000",timeWindowUsed,rfc.RfcSpecifiedNetworkDelay)\nconsole.log(t)\n
end intellisense**/
/**intellisense(!Hotp)
ComputeHOTP(0/*计数器*/)=验证码\n参数1:整数,计数器
VerifyHotp("__",0)=验证,通过返回true\n参数1:字符串,待验证的码\n参数2:整数,计数器
end intellisense**/
/**intellisense(!Base32)
ToBytes("__")=Base32到字节数组\n参数:字符串,Base32编码
ToString(__)=字节数组到Base32\n参数:字节数组,raw.buffer()
end intellisense**/
/**intellisense(!VerificationWindow)
RfcSpecifiedNetworkDelay=只读属性
end intellisense**/ 附件含有demo演示,可以下载查看
用法:kio整个文件夹解压到lib
上传的附件: