1

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)
DomingoMG
  • 1,521
  • 2
  • 16
  • 36
  • so, what does Dart produce? different output? no output? errors? – Bravo Oct 08 '19 at 02:34
  • Sorry, he updated my answer, I get an error of that value that is not in the range. – DomingoMG Oct 08 '19 at 02:44
  • well, that shows one of 9 possible locations this error has occurred at - any idea which? or how many times the loop has run before you get this error? – Bravo Oct 08 '19 at 02:49
  • 2
    this seems a very complex way to encode a string to base64 ... does dart have any functions to do so? https://stackoverflow.com/questions/15957427/how-do-i-encode-a-dart-string-in-base64 – Bravo Oct 08 '19 at 02:55

2 Answers2

2

Just a word of safety. This password is not encrypted using this function. All your javascript function does is a base64 encode, a representation of the password string in a different character set. This is fully reversible and not a sufficient password encryption. Actually none at all. You can verify here that your "encrypted" password can be decoded to your original password string.

You should read up on how to properly secure and encrypt passwords in this answer.

If you're still interested in how to convert your string to base64 (not an encryption) you can use darts convert package from its core library.

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;
}
Hannes Küttner
  • 498
  • 3
  • 8
  • You're right, it's a simple safety word. On my server, when I use the API, the password goes through that security. Thank you. – DomingoMG Oct 08 '19 at 09:09
0

Javascript function charAt returns an empty string when argument value is out of range. This empty string converts to integer 0. Dart throws an exception that value is not in range. You should handle out of range cases yourself.

Igor Kharakhordin
  • 5,916
  • 1
  • 21
  • 28