From d13097060b9a69dafedf1bc1c50f31427035e17c Mon Sep 17 00:00:00 2001 From: Lukas Date: Sat, 5 Oct 2024 00:04:00 +0500 Subject: [PATCH] feat: add dart implementation for url signing --- url_signer.dart | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 url_signer.dart diff --git a/url_signer.dart b/url_signer.dart new file mode 100644 index 0000000..0df7780 --- /dev/null +++ b/url_signer.dart @@ -0,0 +1,30 @@ +import 'dart:convert'; + +import 'package:crypto/crypto.dart'; // official dart package + +/// Sign a URL with a given crypto key +/// Note that this URL must be properly URL-encoded +String signUrl(String myUrlToSign, String privateKey) { + // parse the url + final url = Uri.parse(myUrlToSign); + + final urlPartToSign = '${url.path}?${url.query}'; + + // Decode the private key into its binary format + final decodedKey = base64Url.decode(privateKey); + + // Create a signature using the private key and the URL-encoded + // string using HMAC SHA1. This signature will be binary. + final bytes = utf8.encode(urlPartToSign); + final hmacSha1 = Hmac(sha1, decodedKey); + final digest = hmacSha1.convert(bytes); + + final encodedSignature = base64Url.encode(digest.bytes); + + return '$myUrlToSign&signature=$encodedSignature'; +} + +void main() { + final signedUrl = signUrl("http://maps.google.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID", 'vNIXE0xscrmjlyV-12Nj_BvUPaw='); + print(signedUrl); +}