|
1 | 1 | using Celeste.Mod.TASHelper.Utils; |
2 | 2 | using Microsoft.Xna.Framework; |
3 | 3 | using Monocle; |
| 4 | +using System.Security.Cryptography; |
4 | 5 | using System.Runtime.CompilerServices; |
| 6 | +using System.Text; |
| 7 | +using System.Diagnostics; |
5 | 8 |
|
6 | 9 | namespace Celeste.Mod.TASHelper.Gameplay.AutoWatchEntity; |
7 | 10 |
|
@@ -210,6 +213,73 @@ public static string ToCode(int angle, int offset) { |
210 | 213 | } |
211 | 214 | return DashCodes[num]; |
212 | 215 | } |
| 216 | + |
| 217 | + public static class AurorasHashedDashCode { |
| 218 | + |
| 219 | + public static int AcceptableLengthUpperBound = 8; |
| 220 | + |
| 221 | + private static readonly Dictionary<string, string> results = new Dictionary<string, string>(); |
| 222 | + |
| 223 | + private static readonly HashSet<string> failures = new HashSet<string>(); |
| 224 | + |
| 225 | + private static readonly string[] DashCodesWithoutBlank = ["L", "UL", "U", "UR", "R", "DR", "D", "DL"]; // we assume "" is not used |
| 226 | + private static byte[] AurorasGetHash(string inputString) { |
| 227 | + using HashAlgorithm hashAlgorithm = SHA256.Create(); |
| 228 | + return hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes("AURORAWASHERE" + inputString)); |
| 229 | + } |
| 230 | + |
| 231 | + private static string AurorasGetHashString(string inputString) { |
| 232 | + StringBuilder stringBuilder = new StringBuilder(); |
| 233 | + byte[] hash = AurorasGetHash(inputString); |
| 234 | + foreach (byte b in hash) { |
| 235 | + stringBuilder.Append(b.ToString("X2")); |
| 236 | + } |
| 237 | + return stringBuilder.ToString(); |
| 238 | + } |
| 239 | + |
| 240 | + public static bool TryGetInputs(string hash, int length, out string inputs) { |
| 241 | + if (results.TryGetValue(hash.ToLowerInvariant(), out string input)) { |
| 242 | + inputs = input; |
| 243 | + return true; |
| 244 | + } |
| 245 | + if (failures.Contains(hash.ToLowerInvariant())) { |
| 246 | + inputs = ""; |
| 247 | + return false; |
| 248 | + } |
| 249 | + if (length > AcceptableLengthUpperBound) { |
| 250 | + inputs = ""; |
| 251 | + return false; |
| 252 | + } |
| 253 | + Stopwatch stopwatch = Stopwatch.StartNew(); |
| 254 | + Logger.Log(LogLevel.Warn, "TASHelper", "Trying to decrypt Auroras' DashcodeHashTrigger, please wait..."); |
| 255 | + string lower = hash.ToLowerInvariant(); |
| 256 | + uint upperBound = (uint)2 << (3 * length - 1); |
| 257 | + for (uint i = 0; i < upperBound; i++) { |
| 258 | + if (AurorasGetHashString(CastToDashCode(i, length)).ToLowerInvariant() == lower) { |
| 259 | + inputs = CastToDashCode(i, length); |
| 260 | + results.Add(lower, inputs); |
| 261 | + stopwatch.Stop(); |
| 262 | + Logger.Log(LogLevel.Warn, "TASHelper", $"Found! {inputs} match, costs {stopwatch.ElapsedMilliseconds} ms."); |
| 263 | + return true; |
| 264 | + } |
| 265 | + } |
| 266 | + inputs = ""; |
| 267 | + failures.Add(lower); |
| 268 | + stopwatch.Stop(); |
| 269 | + Logger.Log(LogLevel.Warn, "TASHelper", $"Not Found! Costs {stopwatch.ElapsedMilliseconds} ms."); |
| 270 | + return false; |
| 271 | + } |
| 272 | + |
| 273 | + private static string CastToDashCode(uint num, int length) { |
| 274 | + List<string> list = new(); |
| 275 | + for (int loop = 0; loop < length; loop++) { |
| 276 | + list.Add(DashCodesWithoutBlank[num % 8]); |
| 277 | + num >>= 3; |
| 278 | + } |
| 279 | + return string.Join(",", list); |
| 280 | + } |
| 281 | + } |
| 282 | + |
213 | 283 | } |
214 | 284 |
|
215 | 285 | internal static class CoroutineFinder { |
|
0 commit comments