Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: modernize base types and interfaces #87

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading