Skip to content

Commit

Permalink
(chocolatey#1144) Add HashConverter class
Browse files Browse the repository at this point in the history
This class provides a method to convert base64 hashes into hex form,
returning the input if it is already in hex. It also returns the type
of the hash. Both are based on the length of the input hash. Sha1,
Sha256 and Sha512 are supported.
  • Loading branch information
TheCakeIsNaOH authored and gep13 committed Apr 26, 2024
1 parent db9ac3d commit 3e7fdca
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
<Compile Include="infrastructure.app\utility\ArgumentsUtility.cs" />
<Compile Include="infrastructure.app\utility\PackageUtility.cs" />
<Compile Include="infrastructure.app\validations\SystemStateValidation.cs" />
<Compile Include="infrastructure\cryptography\HashConverter.cs" />
<Compile Include="infrastructure\filesystem\FileSystem.cs" />
<Compile Include="FileSystemExtensions.cs" />
<Compile Include="infrastructure\information\ExtensionInformation.cs" />
Expand Down
65 changes: 65 additions & 0 deletions src/chocolatey/infrastructure/cryptography/HashConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// 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 System;

namespace chocolatey.infrastructure.cryptography
{
public static class HashConverter
{
public static (string convertedHash, CryptoHashProviderType hashType) ConvertHashToHex(string hash)
{
// Sha 1 in base64
if (hash.Length.Equals(28))
{
return (BitConverter.ToString(Convert.FromBase64String(hash)).Replace("-", string.Empty), CryptoHashProviderType.Sha1);
}

// Sha 1 in hex
if (hash.Length.Equals(40))
{
return (hash, CryptoHashProviderType.Sha1);
}

// Sha 256 in base64
if (hash.Length.Equals(44))
{
return (BitConverter.ToString(Convert.FromBase64String(hash)).Replace("-", string.Empty), CryptoHashProviderType.Sha256);
}

// Sha 256 in hex
if (hash.Length.Equals(64))
{
return (hash, CryptoHashProviderType.Sha256);
}

// Sha 512 in base64
if (hash.Length.Equals(88))
{
return (BitConverter.ToString(Convert.FromBase64String(hash)).Replace("-", string.Empty), CryptoHashProviderType.Sha512);
}

// Sha 512 in hex
if (hash.Length.Equals(128))
{
return (hash, CryptoHashProviderType.Sha512);
}

"chocolatey".Log().Warn("Unknown Hash type, Length '{0}', Hash '{1}'".FormatWith(hash, hash.Length));
return (hash, CryptoHashProviderType.Unknown);
}
}
}

0 comments on commit 3e7fdca

Please sign in to comment.