Skip to content

Instantly share code, notes, and snippets.

@bespalown
Last active August 29, 2015 14:23
Show Gist options
  • Save bespalown/96b612c4a8c67a6a2a7d to your computer and use it in GitHub Desktop.
Save bespalown/96b612c4a8c67a6a2a7d to your computer and use it in GitHub Desktop.
/*
var messageToSign = string.Format("{0}{1}{2}{3}", urlToSign,merchantId, timestamp, body);
public static string CreateMerchantSign(string message)
{
byte[] data = Encoding.UTF8.GetBytes(message);
byte[] binaryKey = Encoding.UTF8.GetBytes(MerchantSignatire);
return Convert.ToBase64String(MD5.Create().ComputeHash(Union(data, binaryKey)));
}
*/
//задаю все значения
let urlToSign: String = "https://wl.walletone.com/w1/invoicingapi/invoices"
let merchantId = String(191238281216)
let timestamp: String = "2015-06-17T14:43:33"
let body: String = "{\"Amount\":\"2.91\",\"CurrencyId\":\"643\",\"PaymentTypeId\":\"YandexMoneyRUB\",\"OrderId\":\"GQYOE-MJNPO-49623\",\"MerchantUserId\":\"\(merchantId)\",\"InvoiceAdditionalParams\": [\"WMI_CUSTOMER_ID\":\"testuser1\",\"WMI_CUSTOMER_EMAIL\":\"[email protected]\"]}"
let keyMd5 = "644c7b62757b675b67787352664277605672435c636677315f7867"
var messageToSign = urlToSign + merchantId + timestamp + body
self.createMerchantSign(messageToSign, key:keyMd5)
func createMerchantSign(message: String, key: String) -> String {
let data = [UInt8](message.utf8)
//300 значений: [104, 116, 116, 112, 115, 58, 47, 47, 119, 108, 46, 119...
let binaryKey = [UInt8](key.utf8)
//54 значения: [54, 52, 52, 99, 55, 98, 54, 50, 55, 53, 55, 98...
let union = Set(data + binaryKey)
//объединил 2 множества ([91, 93, 77, 49, 74, 68, 99, 102, 58, 47, 67, 46, 84, 108, 50, 34, 81, 95, 125, 103, 118, 78, 45, 116, 115, 51, 64, 104, 110, 73, 66, 105, 98, 79, 65, 101, 123, 117, 71, 87, 82, 83, 76, 109, 114, 44, 80, 111, 121, 55, 100, 52, 48, 97, 56, 54, 85, 89, 53, 112, 57, 69, 119])
let computeHash = union.hashValue
// 4655881812404811768
let md5 = String(computeHash).md5
// 207c2530d5c760541d47b8599e312da7
let plainData = md5.dataUsingEncoding(NSUTF8StringEncoding)
let base64 = plainData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
// MjA3YzI1MzBkNWM3NjA1NDFkNDdiODU5OWUzMTJkYTc=
return base64
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment