在Dart(Flutter)中以sha256方式将两个字符串附加并哈希为十六进制:

huangapple go评论69阅读模式
英文:

Append and hash two Strings as Hex with sha256 in Dart(Flutter)

问题

以下是您提供的JavaScript代码的翻译部分:

import 'dart:convert';
import 'package:crypto/crypto.dart';

void main() {
  final secret = '<SomeSecretHexNumbers>';
  final id = 123456789;
  final idHex = id.toRadixString(16);

  if (idHex.length % 2 != 0) {
    idHex = '0' + idHex;
  }

  final hash = sha256.convert(Uint8List.fromList(hex.decode(idHex + secret)));

  final expectedHash = hex.encode(hash.bytes);

  print('Hash: $expectedHash');
}

请注意,上述Dart代码使用了crypto库来执行与JavaScript代码相同的功能。不过,您需要确保在Flutter项目中引入crypto库,以便使用sha256和相关功能。

英文:

I have this function in JavaScript and I want to have the same functionality in my Flutter App, but I can't find something similar:

const {createHash} = require(&quot;crypto&quot;)

const secret = &quot;&lt;SomeSecretHexNumbers&gt;&quot;
let id = 123456789
let idHex = id.toString(16)

if (idHex.length % 2 != 0) {
    idHex = &quot;0&quot; + idHex
}

let hash = createHash(&quot;sha256&quot;)
hash.update(Buffer.from(idHex, &quot;hex&quot;))
hash.update(Buffer.from(secret, &quot;hex&quot;))

const expectedHash = hash.digest(&quot;hex&quot;)

console.log(&quot;Hash: &quot; + expectedHash)

I tried to do this in Dart but I couldn't find a way to do it.

答案1

得分: 0

Dart的十六进制编码在package:convert中(不要与dart:convert混淆)。由于您已经有示例代码,您应该逐行分析它,了解它的功能,并在Dart中创建相同的代码,然后进行并行测试。

sha256摘要的文档位于crypto包的首页,包括有关如何对多个字节数组进行哈希的示例。

例如:

import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';

void main() {
  const secret = 'cafe1234';
  final id = 123456789;
  final idHex = id.toRadixString(16);

  final u1 = hex.decode(idHex.padLeft((idHex.length + 1) ~/ 2 * 2, '0'));
  final u2 = hex.decode(secret);

  final output = AccumulatorSink<Digest>();
  sha256.startChunkedConversion(output)
    ..add(u1)
    ..add(u2)
    ..close();
  final expectedHash = hex.encode(output.events.single.bytes);

  print(expectedHash);
}
英文:

Dart's hex codex is in package:convert (not to be confused with dart:convert). As you have the sample code, you should just work through each line seeing what it does, creating the same in Dart and test side by side.

Documentation for the sha256 digest is in the front page of the crypto package, with example for hashing more than one byte array.

For example:

import &#39;package:convert/convert.dart&#39;;
import &#39;package:crypto/crypto.dart&#39;;

void main() {
  const secret = &#39;cafe1234&#39;;
  final id = 123456789;
  final idHex = id.toRadixString(16);

  final u1 = hex.decode(idHex.padLeft((idHex.length + 1) ~/ 2 * 2, &#39;0&#39;));
  final u2 = hex.decode(secret);

  final output = AccumulatorSink&lt;Digest&gt;();
  sha256.startChunkedConversion(output)
    ..add(u1)
    ..add(u2)
    ..close();
  final expectedHash = hex.encode(output.events.single.bytes);

  print(expectedHash);
}

huangapple
  • 本文由 发表于 2023年3月3日 20:26:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定