是否可以在Dart中执行相同的JavaScript函数?我正在查看附加组件,发现了一个与我尝试做的非常接近的补充。
找到的补码是:https : //pub.dev/packages/js_shims
你可以在这里阅读更多。文档:JS_SHIMS
该函数必须返回加密的密码。例如:
密码:pass1234
在javascript中,加密的密码使我返回:cGFzczEyMzQ =
我的功能JS DEMO:
var ezEncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function ezEncode(){
var str = document.getElementById('txtPassword').value;
var out, i, len;
var c1, c2, c3;
len = str.length;
i = 0;
out = "";
while(i < len)
{
c1 = str.charCodeAt(i++) & 0xff;
if(i == len)
{
out += ezEncodeChars.charAt(c1 >> 2);
out += ezEncodeChars.charAt((c1 & 0x3) << 4);
out += "==";
break;
}
c2 = str.charCodeAt(i++);
if(i == len)
{
out += ezEncodeChars.charAt(c1 >> 2);
out += ezEncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += ezEncodeChars.charAt((c2 & 0xF) << 2);
out += "=";
break;
}
c3 = str.charCodeAt(i++);
out += ezEncodeChars.charAt(c1 >> 2);
out += ezEncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += ezEncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
out += ezEncodeChars.charAt(c3 & 0x3F);
}
document.getElementById('txtPassword').value = out;
return out;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="text" hint="pass1234" value="pass1234" id="txtPassword"/>
<input type="button" value="Send" onclick="ezEncode('pass1234')"/>
</body>
</html>
我的飞镖功能
var ezEncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
ezEncode({String password}) {
var out, i, len;
var c1, c2, c3;
len = password.length;
i = 0;
out = "";
while(i < len)
{
c1 = js.charCodeAt(password, i++) & 0xff;
if(i == len)
{
out += js.charAt(ezEncodeChars, c1 >> 2);
out += js.charAt(ezEncodeChars, (c1 & 0x3) << 4);
out += "==";
break;
}
c2 = js.charCodeAt(password, i++);
if(i == len)
{
out += js.charAt(ezEncodeChars, c1 >> 2);
out += js.charAt(ezEncodeChars, ((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += js.charAt(ezEncodeChars, (c2 & 0xF) << 2);
out += "=";
break;
}
c3 = js.charCodeAt(password, i++);
out += js.charAt(ezEncodeChars, c1 >> 2);
out += js.charAt(ezEncodeChars, ((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += js.charAt(ezEncodeChars, ((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
out += js.charAt(ezEncodeChars, c3 & 0x3F);
}
print(out);
}
控制台运行
E/flutter ( 4805): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: RangeError: Value not in range: 28
E/flutter ( 4805): #0 _StringBase.substring (dart:core-patch/string_patch.dart:392:7)
E/flutter ( 4805): #1 charAt (package:js_shims/src/strings.dart:5:45)
只是一个安全的提示。此密码未使用此功能加密。您所有的javascript函数所做的都是base64编码,它是用不同字符集表示的密码字符串。这是完全可逆的,并且没有足够的密码加密。实际上根本没有。您可以在此处验证您的“加密”密码可以解码为原始密码字符串。
您应该在此答案中阅读如何正确保护和加密密码。
如果您仍然对如何将字符串转换为base64(而不是加密)感兴趣,则可以使用其核心库中的darts convert包。
https://api.dartlang.org/stable/2.1.0/dart-convert/dart-convert-library.html
import 'dart:convert';
ezEncodeChars(String notAPassword) {
var bytes = utf8.encode(notAPassword);
var base64Str = base64.encode(bytes);
return base64Str;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句