Security.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Paddings;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  14. namespace crypto
  15. {
  16. public class Security
  17. {
  18. // USAGE
  19. //var key = Security.GenerateText(32);
  20. //var iv = Security.GenerateText(16);
  21. //var encrypted = Security.Encrypt("MY SECRET", key, iv);
  22. //var decrypted = Security.Decrypt(encrypted, key, iv);
  23. /// <summary>
  24. /// Return a salted hash based on PBKDF2 for the UTF-8 encoding of the argument text.
  25. /// </summary>
  26. /// <param name="text">Provided key text</param>
  27. /// <param name="salt">Base64 encoded string representing the salt</param>
  28. /// <returns></returns>
  29. public static string ComputeHash(string text, string salt)
  30. {
  31. byte[] data = Encoding.UTF8.GetBytes(text);
  32. Sha512Digest sha = new Sha512Digest();
  33. Pkcs5S2ParametersGenerator gen = new Pkcs5S2ParametersGenerator(sha);
  34. gen.Init(data, Base64.Decode(salt), 2048);
  35. return Base64.ToBase64String(((KeyParameter)gen.GenerateDerivedParameters(sha.GetDigestSize() * 8)).GetKey());
  36. }
  37. public static string Decrypt(string cipherText, string key, string iv)
  38. {
  39. IBufferedCipher cipher = CreateCipher(false, key, iv);
  40. byte[] textAsBytes = cipher.DoFinal(Base64.Decode(cipherText));
  41. return Encoding.UTF8.GetString(textAsBytes, 0, textAsBytes.Length);
  42. }
  43. public static string Encrypt(string plainText, string key, string iv)
  44. {
  45. IBufferedCipher cipher = CreateCipher(true, key, iv);
  46. return Base64.ToBase64String(cipher.DoFinal(Encoding.UTF8.GetBytes(plainText)));
  47. }
  48. public static string GenerateText(int size)
  49. {
  50. byte[] textAsBytes = new byte[size];
  51. SecureRandom secureRandom = SecureRandom.GetInstance("SHA256PRNG", true);
  52. secureRandom.NextBytes(textAsBytes);
  53. return Base64.ToBase64String(textAsBytes);
  54. }
  55. private static IBufferedCipher CreateCipher(bool isEncryption, string key, string iv)
  56. {
  57. IBufferedCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new RijndaelEngine()), new ISO10126d2Padding());
  58. KeyParameter keyParam = new KeyParameter(Base64.Decode(key));
  59. ICipherParameters cipherParams = (null == iv || iv.Length < 1)
  60. ? (ICipherParameters)keyParam
  61. : new ParametersWithIV(keyParam, Base64.Decode(iv));
  62. cipher.Init(isEncryption, cipherParams);
  63. return cipher;
  64. }
  65. }
  66. }
  67. #pragma warning restore
  68. #endif