|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
| 6 | +using System.IO; |
6 | 7 | using System.Linq;
|
| 8 | +using System.Runtime.Serialization.Formatters.Binary; |
| 9 | +using System.Security.Cryptography; |
| 10 | +using System.Text; |
7 | 11 | using Microsoft.OpenApi.Exceptions;
|
8 | 12 | using Microsoft.OpenApi.Interfaces;
|
9 | 13 | using Microsoft.OpenApi.Services;
|
@@ -62,6 +66,8 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
|
62 | 66 | /// </summary>
|
63 | 67 | public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
|
64 | 68 |
|
| 69 | + private static readonly object locker = new(); |
| 70 | + |
65 | 71 | /// <summary>
|
66 | 72 | /// Parameter-less constructor
|
67 | 73 | /// </summary>
|
@@ -375,26 +381,42 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
|
375 | 381 | return ResolveReference(reference, false);
|
376 | 382 | }
|
377 | 383 |
|
378 |
| - /// <summary> |
379 |
| - /// Computes the hash code for an OpenApiDocument and its property values. |
380 |
| - /// </summary> |
381 |
| - /// <returns> The hash code.</returns> |
382 |
| - public override int GetHashCode() |
| 384 | + ///// <summary> |
| 385 | + ///// Computes the hash code for an OpenApiDocument and its property values. |
| 386 | + ///// </summary> |
| 387 | + ///// <returns> The hash code.</returns> |
| 388 | + //public override int GetHashCode() |
| 389 | + //{ |
| 390 | + // // select two random prime numbers e.g 1 and 3 and use them to compute hash codes |
| 391 | + // int hash = 1; |
| 392 | + // hash = hash * 3 + (Workspace == null ? 0 : Workspace.GetHashCode()); |
| 393 | + // hash = hash * 3 + (Info == null ? 0 : Info.GetHashCode()); |
| 394 | + // hash = hash * 3 + (Servers == null ? 0 : Servers.GetHashCode()); |
| 395 | + // hash = hash * 3 + (Paths == null ? 0 : Paths.GetHashCode()); |
| 396 | + // hash = hash * 3 + (Components == null ? 0 : Components.GetHashCode()); |
| 397 | + // hash = hash * 3 + (SecurityRequirements == null ? 0 : SecurityRequirements.GetHashCode()); |
| 398 | + // hash = hash * 3 + (Tags == null ? 0 : Tags.GetHashCode()); |
| 399 | + // hash = hash * 3 + (ExternalDocs == null ? 0 : ExternalDocs.GetHashCode()); |
| 400 | + // hash = hash * 3 + (Extensions == null ? 0 : Extensions.GetHashCode()); |
| 401 | + |
| 402 | + // return hash; |
| 403 | + //} |
| 404 | + |
| 405 | + public static string GenerateHashValue(Stream input) |
383 | 406 | {
|
384 |
| - // select two random prime numbers e.g 1 and 3 and use them to compute hash codes |
385 |
| - int hash = 1; |
386 |
| - hash = hash * 3 + (Workspace == null ? 0 : Workspace.GetHashCode()); |
387 |
| - hash = hash * 3 + (Info == null ? 0 : Info.GetHashCode()); |
388 |
| - hash = hash * 3 + (Servers == null ? 0 : Servers.GetHashCode()); |
389 |
| - hash = hash * 3 + (Paths == null ? 0 : Paths.GetHashCode()); |
390 |
| - hash = hash * 3 + (Components == null ? 0 : Components.GetHashCode()); |
391 |
| - hash = hash * 3 + (SecurityRequirements == null ? 0 : SecurityRequirements.GetHashCode()); |
392 |
| - hash = hash * 3 + (Tags == null ? 0 : Tags.GetHashCode()); |
393 |
| - hash = hash * 3 + (ExternalDocs == null ? 0 : ExternalDocs.GetHashCode()); |
394 |
| - hash = hash * 3 + (Extensions == null ? 0 : Extensions.GetHashCode()); |
395 |
| - |
396 |
| - return hash; |
397 |
| - } |
| 407 | + HashAlgorithm sha = SHA512.Create(); |
| 408 | + byte[] result = sha.ComputeHash(input); |
| 409 | + |
| 410 | + // Build the final string by converting each byte |
| 411 | + // into hex and appending it to a StringBuilder |
| 412 | + StringBuilder sb = new StringBuilder(); |
| 413 | + for (int i = 0; i < result.Length; i++) |
| 414 | + { |
| 415 | + sb.Append(result[i].ToString("X2")); |
| 416 | + } |
| 417 | + |
| 418 | + return sb.ToString(); |
| 419 | + } |
398 | 420 |
|
399 | 421 | /// <summary>
|
400 | 422 | /// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
|
|
0 commit comments