1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00
imgproxy/examples/signature.dart

33 lines
982 B
Dart
Raw Normal View History

2020-01-13 15:29:58 +02:00
import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
void main() {
2021-03-15 13:26:38 +02:00
var key = hex.decode("943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881");
var salt = hex.decode("520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5");
var url = "http://img.example.com/pretty/image.jpg";
2020-01-13 15:29:58 +02:00
var resizing_type = 'fill';
var width = 300;
var height = 300;
var gravity = 'no';
var enlarge = 1;
var extension = 'png';
2021-03-15 13:26:38 +02:00
var url_encoded = urlSafeBase64(utf8.encode(url));
var path = "/rs:$resizing_type:$width:$height:$enlarge/g:$gravity/$url_encoded.$extension";
2020-01-13 15:29:58 +02:00
var signature = sign(salt, utf8.encode(path), key);
2021-03-15 13:26:38 +02:00
print("/$signature/$path");
2020-01-13 15:29:58 +02:00
}
String urlSafeBase64(buffer) {
2021-03-15 13:26:38 +02:00
return base64.encode(buffer).replaceAll("=", "").replaceAll("+", "-").replaceAll("/", "_");
2020-01-13 15:29:58 +02:00
}
String sign(salt, path, key) {
2021-03-15 13:26:38 +02:00
var hmac = Hmac(sha256, key);
var digest = hmac.convert(salt + path);
return urlSafeBase64(digest.bytes);
}