Reputation: 63
I'm trying to take the process of converting a secret hmac string. Code on node.js and Dart doe not give same result.
Dart Code:
String credentials = "031f0b737d155ed67bb9a3a174e10f6e";
String stringForTokenGeneration = "/627b533efd2da43909008561/63bfe56832156f9174527aa6?expires=1674823949447";
Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(credentials);
// String decoded = stringToBase64.decode(encoded);
var key = utf8.encode(encoded);
var bytes = utf8.encode(stringForTokenGeneration);
var hmacSha1 = Hmac(sha1, key); // HMAC-SHA256
var digest = hmacSha1.convert(bytes);
_counter=" $digest";
print("HMAC digest as bytes: ${digest.bytes}");
print("HMAC digest as hex string: $digest");
nodejs Code
const crypto = require('crypto');
let proxySecret = "031f0b737d155ed67bb9a3a174e10f6e";
proxySecret = Buffer.from(proxySecret, 'base64');
let stringForTokenGeneration = '/627b533efd2da43909008561/63bfe56832156f9174527aa6?expires=1674823949447';
console.log(`string For Token Generation: ${stringForTokenGeneration}`);
let signature = crypto.createHmac('sha1', proxySecret).update(stringForTokenGeneration).digest('hex');
console.log(`Auth Token: ${signature}`);
I want Dart code to return same signature as node
Upvotes: 0
Views: 304
Reputation: 51751
It looks like you are just missing the conversion to hex at the end. The hex codec is in package:convert
which you will need to add to your pubspec.yaml
.
Here it is using pointycastle
but will likely work with hmac from package:crypto
too.
import 'dart:convert';
import 'dart:typed_data';
import 'package:pointycastle/export.dart';
import 'package:convert/convert.dart';
void main() {
final credentials = '031f0b737d155ed67bb9a3a174e10f6e';
final forTokenGeneration =
'/627b533efd2da43909008561/63bfe56832156f9174527aa6?expires=1674823949447';
final hmac = HMac(SHA1Digest(), 64)
..init(KeyParameter(base64.decode(credentials)));
final sigBytes = hmac.process(utf8.encode(forTokenGeneration) as Uint8List);
final signature = hex.encode(sigBytes);
print(signature); // 7dead8b06e017b9727cb52f5bf36665d840a8126
}
Upvotes: 1