Skip to content
Permalink
Browse files

dart signature example (#320)

  • Loading branch information
PrasannaSRK07 authored and DarthSim committed Jan 13, 2020
1 parent c0fb6b1 commit ec2795e3696852b2f949b28ee51e1e87f96f3660
Showing with 32 additions and 0 deletions.
  1. +32 −0 examples/signature.dart
@@ -0,0 +1,32 @@
import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';

void main() {
var key = hex.decode("943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881");
var salt = hex.decode("520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5");
var url = "http://img.example.com/pretty/image.jpg";
var resizing_type = 'fill';
var width = 300;
var height = 300;
var gravity = 'no';
var enlarge = 1;
var extension = 'png';

var url_encoded = urlSafeBase64(utf8.encode(url));

var path = "/$resizing_type/$width/$height/$gravity/$enlarge/$url_encoded.$extension";

var signature = sign(salt, utf8.encode(path), key);
print("/$signature/$path");
}

String urlSafeBase64(buffer) {
return base64.encode(buffer).replaceAll("=", "").replaceAll("+", "-").replaceAll("/", "_");
}

String sign(salt, path, key) {
var hmac = Hmac(sha256, key);
var digest = hmac.convert(salt + path);
return urlSafeBase64(digest.bytes);
}

0 comments on commit ec2795e

Please sign in to comment.
You can’t perform that action at this time.