/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file 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 Amazon.Runtime.SharedInterfaces.Internal; using Aws.Crt.Checksums; using System; namespace AWSSDK.Extensions.CrtIntegration { /// /// Wrapper for checksum algorithms provided by Aws.Crt.Checksums /// public class CrtChecksums : IChecksumProvider { /// /// Computes a CRC32 hash /// /// Data to hash /// CRC32 hash as a base64-encoded string public string Crc32(byte[] source) => ConvertUintChecksumToBase64(Crc.crc32(source)); /// /// Computes a CRC32 hash /// /// Data to hash /// Previous value of a rolling checksum /// Updated CRC32 hash as 32-bit integer public uint Crc32(byte[] source, uint previous) => Crc.crc32(source, previous); /// /// Computes a CRC32C hash /// /// Data to hash /// CRC32C hash as a base64-encoded string public string Crc32C(byte[] source) => ConvertUintChecksumToBase64(Crc.crc32c(source)); /// /// Computes a CRC32C hash /// /// Data to hash /// Previous value of a rolling checksum /// Updated CRC32C hash as 32-bit integer public uint Crc32C(byte[] source, uint previous) => Crc.crc32c(source, previous); /// /// Converts the 32-bit checksum from CRT into a base-64 string /// /// 32-bit checksum /// Checksum as a base64-encoded string private string ConvertUintChecksumToBase64(uint checksum) { var bytes = BitConverter.GetBytes(checksum); if (BitConverter.IsLittleEndian) { Array.Reverse(bytes); } return Convert.ToBase64String(bytes); } } }