Skip to content

Commit

Permalink
refactor!: modernize base types and interfaces (#87)
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT authored Dec 29, 2023
1 parent e940161 commit 6adeaaa
Show file tree
Hide file tree
Showing 47 changed files with 785 additions and 749 deletions.
59 changes: 0 additions & 59 deletions src/OrasProject.Oras/Constants/OCIMediaTypes .cs

This file was deleted.

123 changes: 0 additions & 123 deletions src/OrasProject.Oras/Content/Content.cs

This file was deleted.

51 changes: 51 additions & 0 deletions src/OrasProject.Oras/Content/Digest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Exceptions;
using System;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

namespace OrasProject.Oras.Content;

internal static class Digest
{
private const string digestRegexPattern = @"[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+";
private static readonly Regex digestRegex = new Regex(digestRegexPattern, RegexOptions.Compiled);

/// <summary>
/// Verifies the digest header and throws an exception if it is invalid.
/// </summary>
/// <param name="digest"></param>
internal static string Validate(string digest)
{
if (string.IsNullOrEmpty(digest) || !digestRegex.IsMatch(digest))
{
throw new InvalidDigestException($"Invalid digest: {digest}");
}
return digest;
}

/// <summary>
/// Generates a SHA-256 digest from a byte array.
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
internal static string ComputeSHA256(byte[] content)
{
using var sha256 = SHA256.Create();
var hash = sha256.ComputeHash(content);
var output = $"sha256:{BitConverter.ToString(hash).Replace("-", "")}";
return output.ToLower();
}
}
57 changes: 0 additions & 57 deletions src/OrasProject.Oras/Content/DigestUtility.cs

This file was deleted.

Loading

0 comments on commit 6adeaaa

Please sign in to comment.