No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TangleObfuscator.cs 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace UnityEditor.Purchasing
  5. {
  6. /// <summary>
  7. /// This class will generate the tangled signature used for client-side receipt validation obfuscation.
  8. /// </summary>
  9. public static class TangleObfuscator
  10. {
  11. /// <summary>
  12. /// An Exception thrown when the tangle order array provided is invalid or shorter than the number of data slices made.
  13. /// </summary>
  14. public class InvalidOrderArray : Exception { }
  15. /// <summary>
  16. /// Generates the obfucscation tangle data.
  17. /// </summary>
  18. /// <param name="data"> The Apple or GooglePlay public key data to be obfuscated. </param>
  19. /// <param name="order"> The array, passed by reference, of order of the data slices used to obfuscate the data with. </param>
  20. /// <param name="rkey"> Outputs the encryption key to deobfuscate the tangled data at runtime </param>
  21. /// <returns>The obfucated public key</returns>
  22. public static byte[] Obfuscate(byte[] data, int[] order, out int rkey)
  23. {
  24. var rnd = new Random();
  25. var key = rnd.Next(2, 255);
  26. var res = new byte[data.Length];
  27. var slices = data.Length / 20 + 1;
  28. if (order == null || order.Length < slices)
  29. {
  30. throw new InvalidOrderArray();
  31. }
  32. Array.Copy(data, res, data.Length);
  33. for (var i = 0; i < slices - 1; i++)
  34. {
  35. var j = rnd.Next(i, slices - 1);
  36. order[i] = j;
  37. var sliceSize = 20; // prob should be configurable
  38. var tmp = res.Skip(i * 20).Take(sliceSize).ToArray(); // tmp = res[i*20 .. slice]
  39. Array.Copy(res, j * 20, res, i * 20, sliceSize); // res[i] = res[j*20 .. slice]
  40. Array.Copy(tmp, 0, res, j * 20, sliceSize); // res[j] = tmp
  41. }
  42. order[slices - 1] = slices - 1;
  43. rkey = key;
  44. return res.Select(x => (byte)(x ^ key)).ToArray();
  45. }
  46. }
  47. }