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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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-detect", string expireUnit = "never", int expireLength = 1, string password = "", bool hide = false)
  14. {
  15. TeknikEntities db = new TeknikEntities();
  16. Config config = Config.Load();
  17. Models.Paste paste = db.Pastes.Create();
  18. paste.DatePosted = DateTime.Now;
  19. paste.MaxViews = 0;
  20. paste.Views = 0;
  21. // Generate random url
  22. string url = Utility.RandomString(config.PasteConfig.UrlLength);
  23. while (db.Pastes.Where(p => p.Url == url).FirstOrDefault() != null)
  24. {
  25. url = Utility.RandomString(config.PasteConfig.UrlLength);
  26. }
  27. paste.Url = url;
  28. // Figure out the expire date (null if 'never' or 'visit')
  29. switch (expireUnit.ToLower())
  30. {
  31. case "never":
  32. break;
  33. case "view":
  34. paste.MaxViews = expireLength;
  35. break;
  36. case "minute":
  37. paste.ExpireDate = paste.DatePosted.AddMinutes(expireLength);
  38. break;
  39. case "hour":
  40. paste.ExpireDate = paste.DatePosted.AddHours(expireLength);
  41. break;
  42. case "day":
  43. paste.ExpireDate = paste.DatePosted.AddDays(expireLength);
  44. break;
  45. case "month":
  46. paste.ExpireDate = paste.DatePosted.AddMonths(expireLength);
  47. break;
  48. case "year":
  49. paste.ExpireDate = paste.DatePosted.AddYears(expireLength);
  50. break;
  51. default:
  52. break;
  53. }
  54. // Set the hashed password if one is provided and encrypt stuff
  55. if (!string.IsNullOrEmpty(password))
  56. {
  57. string key = Utility.RandomString(config.PasteConfig.KeySize / 8);
  58. string iv = Utility.RandomString(config.PasteConfig.BlockSize / 8);
  59. paste.HashedPassword = Helpers.SHA384.Hash(key, password);
  60. // Encrypt Content
  61. byte[] data = Encoding.Unicode.GetBytes(content);
  62. byte[] ivBytes = Encoding.Unicode.GetBytes(iv);
  63. byte[] keyBytes = AES.CreateKey(password, ivBytes, config.PasteConfig.KeySize);
  64. byte[] encData = AES.Encrypt(data, keyBytes, ivBytes);
  65. content = Encoding.Unicode.GetString(encData);
  66. paste.Key = key;
  67. paste.KeySize = config.PasteConfig.KeySize;
  68. paste.IV = iv;
  69. paste.BlockSize = config.PasteConfig.BlockSize;
  70. }
  71. paste.Content = content;
  72. paste.Title = title;
  73. paste.Syntax = syntax;
  74. paste.Hide = hide;
  75. return paste;
  76. }
  77. public static bool CheckExpiration(Models.Paste paste)
  78. {
  79. if (paste.ExpireDate != null && DateTime.Now >= paste.ExpireDate)
  80. return true;
  81. if (paste.MaxViews > 0 && paste.Views > paste.MaxViews)
  82. return true;
  83. return false;
  84. }
  85. }
  86. }