-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathurlsigner.pl
76 lines (55 loc) · 1.77 KB
/
urlsigner.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl
#
# Copyright (c) 2010 iGLASS Networks (http://www.iglass.net). All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
# The program contained herein is provided to you "AS IS" without any
# warranties of any kind.
#
# Required Perl Libraries.
use Digest::HMAC_SHA1;
use MIME::Base64;
use URI::URL;
use 5.006;
use strict;
use warnings;
sub decode_urlsafe_base64 {
my($content) = @_;
# replace '+' by '-', and '/' by '_'
$content =~ tr/-/\+/;
$content =~ tr/_/\//;
return decode_base64($content);
}
sub encode_urlsafe{
my($content) = @_;
$content =~ tr/\+/\-/;
$content =~ tr/\//\_/;
return $content;
}
sub sign_url {
my($url) = @_;
# Private Key - Replace with your private key.
my $KEY_STRING = 'vNIXE0xscrmjlyV-12Nj_BvUPaw=';
# Decode the private key into original binary format.
my $key = decode_urlsafe_base64($KEY_STRING);
# Take only the query part
my $parsed_url = URI::URL->new($url);
my $url_to_sign = $parsed_url->path_query;
# Create HMAC-SHA1 Signature
my $digest = Digest::HMAC_SHA1->new($key);
$digest->add($url_to_sign);
# Encode binary signature.
my $signature = $digest->b64digest;
# Make Signature URL safe
$signature = encode_urlsafe($signature);
return ( $parsed_url->scheme .'://' . $parsed_url->host .
$url_to_sign .'&signature=' . $signature )
}
#
# The following is an example from Google w/ result
# See http://code.google.com/apis/maps/documentation/premier/guide.html#SignatureExamples
#
# Propertly encoded URL - Replace with your URL to be signed.
my $url = "http://maps.google.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID";
my $signed_url = sign_url($url);
print $signed_url . "\n";