Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dart implementation for url signing #37

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions url_signer.dart
Original file line number Diff line number Diff line change
@@ -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);
}