The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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

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