【发布时间】:2019-10-08 02:07:24
【问题描述】:
是否可以在 Dart 中执行相同的 JavaScript 功能? 我正在查看附加组件,并找到了与我正在尝试做的接近的补充。
找到的补码是: https://pub.dev/packages/js_shims
您可以在此处阅读更多内容。 Documentation: 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)
【问题讨论】:
-
那么,Dart 产生了什么?不同的输出?没有输出?错误?
-
对不起,他更新了我的答案,我得到一个不在范围内的值的错误。
-
嗯,这显示了发生此错误的 9 个可能位置之一 - 知道哪个吗?或者在出现此错误之前循环运行了多少次?
-
这似乎是一种将字符串编码为 base64 的非常复杂的方法...... dart 是否有任何功能可以做到这一点? stackoverflow.com/questions/15957427/…
标签: javascript flutter dart