public bool IsValidHmac(NotificationRequestItem notificationRequestItem, string key) { string expectedSign = CalculateHmac(notificationRequestItem, key); string merchantSign = notificationRequestItem.AdditionalData[Constants.AdditionalData.HmacSignature]; return(string.Equals(expectedSign, merchantSign)); }
public void TestNotificationRequestItemHmac() { string key = "DFB1EB5485895CFA84146406857104ABB4CBCABDC8AAF103A624C8F6A3EAAB00"; var expectedSign = "ipnxGCaUZ4l8TUW75a71/ghd2Fe5ffvX0pV4TLTntIc="; var additionalData = new Dictionary <string, string> { { Constants.AdditionalData.HmacSignature, expectedSign } }; var notificationRequestItem = new NotificationRequestItem { PspReference = "pspReference", OriginalReference = "originalReference", MerchantAccountCode = "merchantAccount", MerchantReference = "reference", Amount = new Model.Amount("EUR", 1000), EventCode = "EVENT", Success = true, AdditionalData = additionalData }; var hmacValidator = new HmacValidator(); var data = hmacValidator.GetDataToSign(notificationRequestItem); Assert.AreEqual("pspReference:originalReference:merchantAccount:reference:1000:EUR:EVENT:true", data); var encrypted = hmacValidator.CalculateHmac(notificationRequestItem, key); Assert.AreEqual(expectedSign, encrypted); notificationRequestItem.AdditionalData[Constants.AdditionalData.HmacSignature] = expectedSign; Assert.IsTrue(hmacValidator.IsValidHmac(notificationRequestItem, key)); notificationRequestItem.AdditionalData[Constants.AdditionalData.HmacSignature] = "notValidSign"; Assert.IsFalse(hmacValidator.IsValidHmac(notificationRequestItem, key)); }
public string GetDataToSign(NotificationRequestItem notificationRequestItem) { var amount = notificationRequestItem.Amount; var signedDataList = new List <string> { notificationRequestItem.PspReference, notificationRequestItem.OriginalReference, notificationRequestItem.MerchantAccountCode, notificationRequestItem.MerchantReference, Convert.ToString(amount.Value), amount.Currency, notificationRequestItem.EventCode, notificationRequestItem.Success.ToString().ToLower() }; return(String.Join(":", signedDataList)); }
public string CalculateHmac(NotificationRequestItem notificationRequestItem, string key) { var notificationRequestItemData = GetDataToSign(notificationRequestItem); return(CalculateHmac(notificationRequestItemData, key)); }