Is it possible to perform the same JavaScript function in Dart? I was looking at the add-ons and found a complement that is close to what I am trying to do.
The complement found was: https://pub.dev/packages/js_shims
You can read more here. Documentation: JS_SHIMS
The function must return the encrypted password. For example:
Password: pass1234
In javascript the encrypted password returns me to: cGFzczEyMzQ=
My function 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>
My dart function
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);
}
CONSOLE RUN
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)