DomingoMG
DomingoMG

Reputation: 1857

Convert JavaScript function to Dart: Word of safety

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)

Upvotes: 1

Views: 1976

Answers (2)

Hannes K&#252;ttner
Hannes K&#252;ttner

Reputation: 978

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;
}

Upvotes: 2

Igor Kharakhordin
Igor Kharakhordin

Reputation: 9873

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.

Upvotes: 0

Related Questions