StringUtils.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using UnityEngine;
  3. public class StringUtils {
  4. public static string getRandomAlphanumericString(int __length = 1) {
  5. // Returns a random alphanumeric string with the specific number of chars
  6. string chars = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
  7. int i = 0;
  8. string str = "";
  9. for (i = 0; i < __length; i++) {
  10. str += chars.Substring((int)Math.Round((float)UnityEngine.Random.Range(0, chars.Length-1)), 1);
  11. }
  12. return str;
  13. }
  14. // http://wiki.unity3d.com/index.php?title=MD5
  15. public static string MD5(string strToEncrypt) {
  16. System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
  17. byte[] bytes = ue.GetBytes(strToEncrypt);
  18. // encrypt bytes
  19. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  20. byte[] hashBytes = md5.ComputeHash(bytes);
  21. // Convert the encrypted bytes back to a string (base 16)
  22. string hashString = "";
  23. for (int i = 0; i < hashBytes.Length; i++) {
  24. hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
  25. }
  26. return hashString.PadLeft(32, '0');
  27. }
  28. // Encrypts a string using a key and the RC4 algorithm
  29. // http://entitycrisis.blogspot.com/2011/04/encryption-between-python-and-c.html
  30. // Test: http://rc4.online-domain-tools.com/
  31. public static byte[] encodeRC4(string __data, string __skey) {
  32. byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(__data);
  33. var key = System.Text.ASCIIEncoding.ASCII.GetBytes(__skey);
  34. byte[] s = new byte[256];
  35. byte[] k = new byte[256];
  36. byte temp;
  37. int i, j;
  38. for (i = 0; i < 256; i++) {
  39. s[i] = (byte)i;
  40. k[i] = key[i % key.GetLength (0)];
  41. }
  42. j = 0;
  43. for (i = 0; i < 256; i++) {
  44. j = (j + s[i] + k[i]) % 256;
  45. temp = s[i];
  46. s[i] = s[j];
  47. s[j] = temp;
  48. }
  49. i = j = 0;
  50. for (int x = 0; x < bytes.GetLength (0); x++) {
  51. i = (i + 1) % 256;
  52. j = (j + s[i]) % 256;
  53. temp = s[i];
  54. s[i] = s[j];
  55. s[j] = temp;
  56. int t = (s[i] + s[j]) % 256;
  57. bytes[x] ^= s[t];
  58. }
  59. return bytes;
  60. }
  61. public static string convertBytesToString(byte[] bytes) {
  62. // Converts bytes to a string representation (e.g. ffd4a0)
  63. string result = "";
  64. for (int i = 0; i < bytes.Length; i++) {
  65. result += bytes[i].ToString("x2");
  66. }
  67. return result;
  68. }
  69. public static string convertIntToCustomBase(int __number, string __charset, int __padSize = 0) {
  70. // Convert a number to any base given a charset
  71. // Radix, as per http://www.newgrounds.com/wiki/creator-resources/newgrounds-apis/encryption-process
  72. int baseLen = __charset.Length;
  73. int currValue = __number;
  74. int rest = 0;
  75. string result = "";
  76. while (currValue >= baseLen) {
  77. rest = currValue % baseLen;
  78. currValue = (currValue - rest) / baseLen;
  79. result = __charset.Substring(rest, 1) + result;
  80. }
  81. rest = currValue % baseLen;
  82. result = __charset.Substring(rest, 1) + result;
  83. while (result.Length < __padSize) result = __charset.Substring(0,1) + result;
  84. return result;
  85. }
  86. }