The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
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.

PasteHelper.cs 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using Teknik.Configuration;
  7. using Teknik.Helpers;
  8. using Teknik.Models;
  9. namespace Teknik.Areas.Paste
  10. {
  11. public static class PasteHelper
  12. {
  13. public static Models.Paste CreatePaste(string content, string title = "", string syntax = "auto", string expireUnit = "never", int expireLength = 1, string password = "", bool hide = false)
  14. {
  15. Config config = Config.Load();
  16. Models.Paste paste = new Models.Paste();
  17. paste.DatePosted = DateTime.Now;
  18. paste.Url = Utility.RandomString(config.PasteConfig.UrlLength);
  19. paste.MaxViews = 0;
  20. paste.Views = -1;
  21. // Figure out the expire date (null if 'never' or 'visit')
  22. switch (expireUnit.ToLower())
  23. {
  24. case "never":
  25. break;
  26. case "view":
  27. paste.MaxViews = expireLength;
  28. break;
  29. case "minute":
  30. paste.ExpireDate = paste.DatePosted.AddMinutes(expireLength);
  31. break;
  32. case "hour":
  33. paste.ExpireDate = paste.DatePosted.AddHours(expireLength);
  34. break;
  35. case "day":
  36. paste.ExpireDate = paste.DatePosted.AddDays(expireLength);
  37. break;
  38. case "month":
  39. paste.ExpireDate = paste.DatePosted.AddMonths(expireLength);
  40. break;
  41. case "year":
  42. paste.ExpireDate = paste.DatePosted.AddYears(expireLength);
  43. break;
  44. default:
  45. break;
  46. }
  47. // Set the hashed password if one is provided and encrypt stuff
  48. if (!string.IsNullOrEmpty(password))
  49. {
  50. string key = Utility.RandomString(config.PasteConfig.KeySize / 8);
  51. string iv = Utility.RandomString(config.PasteConfig.BlockSize / 8);
  52. paste.HashedPassword = Helpers.SHA384.Hash(key, password);
  53. // Encrypt Content
  54. byte[] data = Encoding.Unicode.GetBytes(content);
  55. byte[] ivBytes = Encoding.Unicode.GetBytes(iv);
  56. byte[] keyBytes = AES.CreateKey(password, ivBytes, config.PasteConfig.KeySize);
  57. byte[] encData = AES.Encrypt(data, keyBytes, ivBytes);
  58. content = Encoding.Unicode.GetString(encData);
  59. paste.Key = key;
  60. paste.KeySize = config.PasteConfig.KeySize;
  61. paste.IV = iv;
  62. paste.BlockSize = config.PasteConfig.BlockSize;
  63. }
  64. paste.Content = content;
  65. paste.Title = title;
  66. paste.Syntax = syntax;
  67. paste.Hide = hide;
  68. return paste;
  69. }
  70. public static bool CheckExpiration(Models.Paste paste)
  71. {
  72. if (paste.ExpireDate != null && DateTime.Now >= paste.ExpireDate)
  73. return true;
  74. if (paste.MaxViews > 0 && paste.Views > paste.MaxViews)
  75. return true;
  76. return false;
  77. }
  78. }
  79. }