简体   繁体   English

Dart和Vuforia的VWS API中的Base64 HMAC-SHA1和MD5加密

[英]Base64 HMAC-SHA1 and MD5 encryption in Dart and Vuforia's VWS API

To communicate with Vuforia through its VWS API I have to do some tricky stuff: first create this string 要通过Vuforia的VWS API与Vuforia通信,我必须做一些棘手的事情:首先创建此字符串

StringToSign = 
  HTTP-Verb + "\n" +
  Content-MD5 + "\n" +
  Content-Type + "\n" +
  Date + "\n" +
  Request-Path;

where Content-MD5 is the encryption of the request's body... Content-MD5是请求正文的加密...

(from the first boundary to the last one, including the boundary itself). (从第一个边界到最后一个边界,包括边界本身)。 For request types without request body, include the MD5 hash of an empty string which is “d41d8cd98f00b204e9800998ecf8427e”. 对于没有请求正文的请求类型,请包括一个空字符串的MD5哈希,即“ d41d8cd98f00b204e9800998ecf8427e”。

then with this string you have to perform the equivalent to this Java code 然后,使用此字符串,您必须执行与此Java代码相同的操作

Signature = Base64 (HMAC-SHA1 (server_secret_key, StringToSign));

where server_secret_key is a constant. 其中server_secret_key是一个常量。 Finally you have to plug that into an authorization header of this form 最后,您必须将其插入此表单的授权标头中

Authorization: VWS {provision_access_key}:{Signature}

I've got no experience with encryption, can anybody tell me how to do this in Dart? 我没有加密方面的经验,有人可以告诉我如何在Dart中进行加密吗?

Edit 编辑

More info about this on Setting Up the API 有关设置API的更多信息

All the algorithms you need for this are in the dart crypto package . 您需要的所有算法都在dart crypto软件包中

import 'dart:convert';
import 'dart:io';

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

main() {
  var contentStr = '{x:"y"}';
  var content = UTF8.encode(contentStr);
  var md5 = new crypto.MD5();
  md5.add(content);

  var verb = 'GET';
  var hash = crypto.CryptoUtils.bytesToHex(md5.close());
  var type = 'text/plain';
  var date = HttpDate.format(new DateTime.now());
  var path = '/request/path';
  var stringToSign = '$verb\n$hash\n$type\n$date\n$path';
  print(stringToSign);
  print('');

  var keyStr = "0102030405060708090a0b0c0d0e0f";
  var key = [];
  for (int i = 0; i < keyStr.length; i += 2) {
    key.add(int.parse(keyStr.substring(i, i + 2), radix: 16));
  }
  var hmac = new crypto.HMAC(new crypto.SHA1(), key);
  hmac.add(UTF8.encode(stringToSign));
  print(crypto.CryptoUtils.bytesToHex(hmac.close()));
}

Of cause you need to figure out the exact encoding of the different parts, eg the date. 当然,您需要找出不同部分的确切编码,例如日期。 If just one bit is wrong in the input nothing works. 如果输入中只有一位错误,则无济于事。

If you have some examples of input and output it is much easier to get the details right. 如果您有一些输入和输出示例,则更容易获得正确的细节。 Eg test the MD5 of the empty string 例如测试空字符串的MD5

print(crypto.CryptoUtils.bytesToHex(new crypto.MD5().close()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM