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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. using Teknik.Utilities.Cryptography;
  10. using Teknik.Data;
  11. using System.IO;
  12. namespace Teknik.Areas.Paste
  13. {
  14. public static class PasteHelper
  15. {
  16. public static Models.Paste CreatePaste(Config config, TeknikEntities db, string content, string title = "", string syntax = "text", string expireUnit = "never", int expireLength = 1, string password = "", bool hide = false)
  17. {
  18. Models.Paste paste = new Models.Paste();
  19. paste.DatePosted = DateTime.Now;
  20. paste.MaxViews = 0;
  21. paste.Views = 0;
  22. // Generate random url
  23. string url = StringHelper.RandomString(config.PasteConfig.UrlLength);
  24. while (db.Pastes.Where(p => p.Url == url).FirstOrDefault() != null)
  25. {
  26. url = StringHelper.RandomString(config.PasteConfig.UrlLength);
  27. }
  28. paste.Url = url;
  29. // Figure out the expire date (null if 'never' or 'visit')
  30. switch (expireUnit.ToLower())
  31. {
  32. case "never":
  33. break;
  34. case "view":
  35. paste.MaxViews = expireLength;
  36. break;
  37. case "minute":
  38. paste.ExpireDate = paste.DatePosted.AddMinutes(expireLength);
  39. break;
  40. case "hour":
  41. paste.ExpireDate = paste.DatePosted.AddHours(expireLength);
  42. break;
  43. case "day":
  44. paste.ExpireDate = paste.DatePosted.AddDays(expireLength);
  45. break;
  46. case "month":
  47. paste.ExpireDate = paste.DatePosted.AddMonths(expireLength);
  48. break;
  49. case "year":
  50. paste.ExpireDate = paste.DatePosted.AddYears(expireLength);
  51. break;
  52. default:
  53. break;
  54. }
  55. if (!Directory.Exists(config.PasteConfig.PasteDirectory))
  56. {
  57. Directory.CreateDirectory(config.PasteConfig.PasteDirectory);
  58. }
  59. // Generate a unique file name that does not currently exist
  60. string filePath = FileHelper.GenerateRandomFileName(config.PasteConfig.PasteDirectory, config.PasteConfig.FileExtension, 10);
  61. string fileName = Path.GetFileName(filePath);
  62. string key = GenerateKey(config.PasteConfig.KeySize);
  63. string iv = GenerateIV(config.PasteConfig.BlockSize);
  64. byte[] ivBytes = Encoding.Unicode.GetBytes(iv);
  65. byte[] keyBytes = AesCounterManaged.CreateKey(key, ivBytes, config.PasteConfig.KeySize);
  66. // Set the hashed password if one is provided and modify the key
  67. if (!string.IsNullOrEmpty(password))
  68. {
  69. paste.HashedPassword = HashPassword(key, password);
  70. keyBytes = AesCounterManaged.CreateKey(password, ivBytes, config.PasteConfig.KeySize);
  71. }
  72. // Encrypt Content
  73. byte[] data = Encoding.Unicode.GetBytes(content);
  74. using (MemoryStream ms = new MemoryStream(data))
  75. {
  76. AesCounterManaged.EncryptToFile(filePath, ms, config.PasteConfig.ChunkSize, keyBytes, ivBytes);
  77. }
  78. // Generate a deletion key
  79. string delKey = StringHelper.RandomString(config.PasteConfig.DeleteKeyLength);
  80. paste.Key = key;
  81. paste.KeySize = config.PasteConfig.KeySize;
  82. paste.IV = iv;
  83. paste.BlockSize = config.PasteConfig.BlockSize;
  84. paste.FileName = fileName;
  85. //paste.Content = content;
  86. paste.Title = title;
  87. paste.Syntax = syntax;
  88. paste.Hide = hide;
  89. paste.DeleteKey = delKey;
  90. return paste;
  91. }
  92. public static bool CheckExpiration(Models.Paste paste)
  93. {
  94. if (paste.ExpireDate != null && DateTime.Now >= paste.ExpireDate)
  95. return true;
  96. if (paste.MaxViews > 0 && paste.Views > paste.MaxViews)
  97. return true;
  98. return false;
  99. }
  100. public static string GenerateKey(int keySize)
  101. {
  102. return StringHelper.RandomString(keySize / 8);
  103. }
  104. public static string GenerateIV(int ivSize)
  105. {
  106. return StringHelper.RandomString(ivSize / 16);
  107. }
  108. public static string HashPassword(string key, string password)
  109. {
  110. return SHA384.Hash(key, password).ToHex();
  111. }
  112. }
  113. }