【问题标题】:Convert JavaScript function to Dart: Word of safety将 JavaScript 函数转换为 Dart:安全词
【发布时间】: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


【解决方案1】:

只是一句安全的话。此密码使用此功能未加密。您所有的 javascript 函数所做的只是一个base64 编码,它以不同的字符集表示密码字符串。这是完全可逆的,并且不是足够的密码加密。实际上根本没有。您可以验证here,您的“加密”密码可以解码为您的原始密码字符串。

您应该在answer 中了解如何正确保护和加密密码。

如果您仍然对如何将字符串转换为 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;
}

【讨论】:

  • 你是对的,这是一个简单的安全词。在我的服务器上,当我使用 API 时,密码会通过该安全性。谢谢。
【解决方案2】:

Javascript 函数 charAt 当参数值超出范围时返回一个空字符串。此空字符串转换为整数 0。Dart 抛出值不在范围内的异常。您应该自己处理超出范围的情况。

【讨论】:

  • charAt 函数永远不会被超出范围的值调用
  • * 除非 dart 的按位运算符(&gt;&gt;&lt;&lt;&amp;|)表现不同,当然
猜你喜欢
  • 2022-06-14
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 2012-01-27
  • 2021-06-27
  • 2014-02-25
相关资源
最近更新 更多