Created
August 29, 2012 16:57
-
-
Save vadimii/3515601 to your computer and use it in GitHub Desktop.
Invoke service with signed content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// load primary key data from PEM file with BouncyCastle lib | |
var privrsa = new RSACryptoServiceProvider(); | |
using (StreamReader sr = new StreamReader("../../rsakeys.pem")) | |
{ | |
var pass = new Password("gDpfk2Em".ToCharArray()); | |
var pr = new PemReader(sr, pass); | |
var p = (AsymmetricCipherKeyPair)pr.ReadObject(); | |
var o = (RsaPrivateCrtKeyParameters)p.Private; | |
var rsaParams = new RSAParameters | |
{ | |
Exponent = o.PublicExponent.ToByteArrayUnsigned(), | |
Modulus = o.Modulus.ToByteArrayUnsigned(), | |
DP = o.DP.ToByteArrayUnsigned(), | |
DQ = o.DQ.ToByteArrayUnsigned(), | |
InverseQ = o.QInv.ToByteArrayUnsigned(), | |
P = o.P.ToByteArrayUnsigned(), | |
Q = o.Q.ToByteArrayUnsigned(), | |
D = o.Exponent.ToByteArrayUnsigned() | |
}; | |
privrsa.ImportParameters(rsaParams); | |
} | |
// take message file content | |
var message = File.ReadAllText("../../message.xml"); | |
// get UTF-8 bytes from message text | |
var messageBytes = Encoding.UTF8.GetBytes(message); | |
// sign message bytes | |
var sign = privrsa.SignData( | |
messageBytes, 0, messageBytes.Length, "SHA1"); | |
// convert sign's bites to the base64 string | |
var b64sing = Convert.ToBase64String(sign); | |
// encode base64 string to URL format | |
var urlb64sing = HttpUtility.UrlEncode(b64sing); | |
// pass encoded sign string to the 'siq' query parameter | |
var fullUrl = "http://test.lift2future.com/external/points?sig=" | |
+ urlb64sing; | |
byte[] response; | |
// call the service | |
using (var br = new WebClient()) | |
{ | |
br.Headers.Add("Content-Type", "application/xml"); | |
response = br.UploadData(fullUrl, "PUT", messageBytes); | |
} | |
// resString is 'the request was successfully processed' | |
// and response code is 201 | |
var resString = Encoding.UTF8.GetString(response); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.PublicKey import RSA | |
from Crypto.Hash import SHA | |
from Crypto.Signature import PKCS1_v1_5 | |
from base64 import b64encode | |
import urllib2 | |
with open('../tryrsa/rsakeys.pem') as f: | |
privkey = f.read() | |
with open('./message.xml') as f: | |
# for example message with ISO 8859-1 character set encoding | |
message = f.read().decode('iso-8859-1') | |
mesbytes = message.encode('utf-8') | |
# create signature | |
key = RSA.importKey(privkey, 'gDpfk2Em') | |
dgst = SHA.new(mesbytes) | |
signer = PKCS1_v1_5.new(key) | |
signature = signer.sign(dgst) | |
# convert signature to URL safe base64 string | |
b64sign = b64encode(signature) | |
url_b64sign = urllib2.quote(b64sign) | |
# call the service | |
service_url = 'http://test.lift2future.com/external/points?sig='+url_b64sign | |
opener = urllib2.build_opener(urllib2.HTTPHandler) | |
request = urllib2.Request(service_url, data=mesbytes) | |
request.add_header('Content-Type', 'application/xml') | |
request.get_method = lambda: 'PUT' | |
resp = opener.open(request) | |
# 201 and 'the request was successfully processed' | |
print resp.read() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<message> | |
<name>Adam Johann Ritter von Krusenstern</name> | |
<email>[email protected]</email> | |
<!-- birthday format is xs:date --> | |
<birthday>1770-11-19</birthday> | |
<tag>my_smart_test_collection/my_super_test</tag> | |
<!-- value format is xs:decimal --> | |
<points>999.50</points> | |
</message> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment