diff --git a/MainScriptingObject.cs b/MainScriptingObject.cs index cf0e332..bbc68f1 100644 --- a/MainScriptingObject.cs +++ b/MainScriptingObject.cs @@ -12,11 +12,11 @@ namespace mScriptableCS25; public class MainScriptingObject : IDisposable { SortedDictionary scrInp1 = new(StringComparer.OrdinalIgnoreCase); - SortedDictionary scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it + SortedDictionary scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it (so we can cache it) string scriptSource = string.Empty; - string tempPath = Path.GetTempPath(); + string tempPath = Path.GetTempPath() + @"TmpVbsScripts" + Path.DirectorySeparatorChar; Dictionary>? dctCache = null; - Dictionary dctFileCache = new(StringComparer.OrdinalIgnoreCase); + Dictionary dctFileCache = new();//(StringComparer.OrdinalIgnoreCase); Random rn = new(); List lstFilesToRemove = new(); bool removedOldScripts = false; @@ -25,6 +25,8 @@ public class MainScriptingObject : IDisposable { if (dctCache != null) dctCache.Clear(); + if (dctFileCache != null) + dctFileCache.Clear(); } public bool HadErrors { get; set; } = false; @@ -60,10 +62,12 @@ public class MainScriptingObject : IDisposable } if (File.Exists(f)) File.Delete(f); + + tempPath = Path.GetDirectoryName(f); } catch { - tempPath = Path.GetTempPath(); + tempPath = Path.GetTempPath() + @"TmpVbsScripts" + Path.DirectorySeparatorChar; } } } @@ -71,11 +75,12 @@ public class MainScriptingObject : IDisposable public bool SetScriptFromFile(string VBScriptPath) { var rv = false; + var theKey = SHA256Hash(VBScriptPath); - if (dctFileCache.ContainsKey(VBScriptPath)) + if (dctFileCache.ContainsKey(theKey)) { - scriptSource = dctFileCache[VBScriptPath].VbsScriptSource; - dctCache = dctFileCache[VBScriptPath].CacheDict; + scriptSource = dctFileCache[theKey].VbsScriptSource; + dctCache = dctFileCache[theKey].CacheDict; rv = true; } else @@ -93,8 +98,9 @@ public class MainScriptingObject : IDisposable scriptSource = sr.ReadToEnd(); z.VbsScriptSource = scriptSource; - dctCache = z.CacheDict; - dctFileCache[VBScriptPath] = z; + z.CacheDict = new(); + dctFileCache[theKey] = z; + dctCache = dctFileCache[theKey].CacheDict; rv = true; } } @@ -142,7 +148,8 @@ public class MainScriptingObject : IDisposable var tStamp = string.Format("TmpVbsScript-{0:yyyyMMddHHmmssfff}-{1}", DateTime.Now, Guid.NewGuid().ToString("N").Substring(0, 12)); var outFile = Path.Combine(tempPath, tStamp + ".txt"); var scriptFile = Path.Combine(tempPath, tStamp + ".vbs"); - var iKey = SHA1Hash(string.Join('\t', scrInp2.ToArray())); + var iClearValues = string.Join('\t', scrInp2.ToArray()); + var iKey = SHA256Hash(iClearValues); var toReturn = new Dictionary(StringComparer.OrdinalIgnoreCase); var retVals = string.Empty; var varName = string.Empty; @@ -151,7 +158,7 @@ public class MainScriptingObject : IDisposable HadErrors = false; lstFilesToRemove.Add(outFile); lstFilesToRemove.Add(scriptFile); - if (UseCache && dctCache.ContainsKey(iKey)) + if (UseCache && dctCache != null && dctCache.ContainsKey(iKey)) toReturn = dctCache[iKey]; else { @@ -173,6 +180,9 @@ public class MainScriptingObject : IDisposable genCodeHeader.AppendLine("'* Revised in 2015 by KeyMotive LLC (Rich Deck) *"); genCodeHeader.AppendLine("'* Refreshed in 2025 by KeyMotive LLC (Rich Deck)*"); genCodeHeader.AppendLine("'*************************************************"); + genCodeHeader.AppendLine("'Non-Crypto Lookup: " + iClearValues); + genCodeHeader.AppendLine("'Crypto Lookup: " + iKey); + genCodeHeader.AppendLine("'*************************************************"); genCodeHeader.AppendLine("outFile = \"" + outFile + "\""); //get our file I/O initialized. This assumes we have write @@ -277,7 +287,7 @@ public class MainScriptingObject : IDisposable } } - if (UseCache) + if (UseCache && dctCache != null) dctCache[iKey] = toReturn; } catch (Exception ex) @@ -338,6 +348,9 @@ public class MainScriptingObject : IDisposable return gx; } + #endregion + + #region "Crypto stuff" internal string SHA1Hash(string strToHash) { var rv = new StringBuilder(); @@ -356,8 +369,61 @@ public class MainScriptingObject : IDisposable return rv.ToString(); } + internal string SHA256Hash(string strToHash) + { + var rv = new StringBuilder(); + + using (var sha = SHA256.Create()) + { + byte[] bytes = Encoding.UTF8.GetBytes(strToHash); + byte[] hash = sha.ComputeHash(bytes); + + foreach (byte x in hash) + { + rv.AppendFormat("{0:x2}", x); + } + } + + return rv.ToString(); + } + internal string SHA384Hash(string strToHash) + { + var rv = new StringBuilder(); + + using (var sha = SHA384.Create()) + { + byte[] bytes = Encoding.UTF8.GetBytes(strToHash); + byte[] hash = sha.ComputeHash(bytes); + + foreach (byte x in hash) + { + rv.AppendFormat("{0:x2}", x); + } + } + + return rv.ToString(); + } + + internal string SHA512Hash(string strToHash) + { + var rv = new StringBuilder(); + + using (var sha = SHA512.Create()) + { + byte[] bytes = Encoding.UTF8.GetBytes(strToHash); + byte[] hash = sha.ComputeHash(bytes); + + foreach (byte x in hash) + { + rv.AppendFormat("{0:x2}", x); + } + } + + return rv.ToString(); + } #endregion + #region "Models and Such" internal class MdlVbsSources : IDisposable { diff --git a/mScriptableCS25.csproj b/mScriptableCS25.csproj index 8aa33e7..a5be167 100644 --- a/mScriptableCS25.csproj +++ b/mScriptableCS25.csproj @@ -4,9 +4,9 @@ net9.0-windows enable enable - 1.2.0.8 - 1.2.0.8 - 1.0.0.8 + 1.2.0.21 + 1.2.0.21 + 1.0.0.21