Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

GooglePlayValidator.cs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Purchasing;
  4. using System.Security.Cryptography;
  5. namespace UnityEngine.Purchasing.Security
  6. {
  7. internal class GooglePlayValidator
  8. {
  9. private RSAKey key;
  10. public GooglePlayValidator(byte[] rsaKey)
  11. {
  12. key = new RSAKey(rsaKey);
  13. }
  14. public GooglePlayReceipt Validate(string receipt, string signature)
  15. {
  16. var rawReceipt = System.Text.Encoding.UTF8.GetBytes(receipt); // "{\"orderId\":\"G...
  17. var rawSignature = System.Convert.FromBase64String(signature);
  18. if (!key.VerifySha1(rawReceipt, rawSignature))
  19. {
  20. throw new InvalidSignatureException();
  21. }
  22. var dic = (Dictionary<string, object>)MiniJson.JsonDecode(receipt);
  23. object orderID, packageName, productId, purchaseToken, purchaseTime, purchaseState;
  24. dic.TryGetValue("orderId", out orderID);
  25. dic.TryGetValue("packageName", out packageName);
  26. dic.TryGetValue("productId", out productId);
  27. dic.TryGetValue("purchaseToken", out purchaseToken);
  28. dic.TryGetValue("purchaseTime", out purchaseTime);
  29. dic.TryGetValue("purchaseState", out purchaseState);
  30. // Google specifies times in milliseconds since 1970.
  31. var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  32. // NOTE: to safely handle null values for these fields, using Convert.ToDouble & ToInt32 in place of casts
  33. var time = epoch.AddMilliseconds(Convert.ToDouble(purchaseTime));
  34. var state = (GooglePurchaseState)Convert.ToInt32(purchaseState);
  35. return new GooglePlayReceipt((string)productId, (string)orderID, (string)packageName,
  36. (string)purchaseToken, time, state);
  37. }
  38. }
  39. }