public void Process(DicomDataset dicomDataset, DicomItem item, ProcessContext context = null) { EnsureArg.IsNotNull(dicomDataset, nameof(dicomDataset)); EnsureArg.IsNotNull(item, nameof(item)); if (item is DicomStringElement) { var hashedValues = ((DicomStringElement)item).Get <string[]>().Select(GetCryptoHashString); dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, hashedValues.ToArray()); } else if (item is DicomOtherByte) { var valueBytes = ((DicomOtherByte)item).Get <byte[]>(); var hashedBytes = _cryptoHashFunction.Hash(valueBytes); dicomDataset.AddOrUpdate(item.ValueRepresentation, item.Tag, hashedBytes); } else if (item is DicomFragmentSequence) { var element = item.ValueRepresentation == DicomVR.OW ? (DicomFragmentSequence) new DicomOtherWordFragment(item.Tag) : new DicomOtherByteFragment(item.Tag); foreach (var fragment in (DicomFragmentSequence)item) { element.Fragments.Add(new MemoryByteBuffer(_cryptoHashFunction.Hash(fragment.Data))); } dicomDataset.AddOrUpdate(element); } else { throw new AnonymizerOperationException(DicomAnonymizationErrorCode.UnsupportedAnonymizationMethod, $"CryptoHash is not supported for {item.ValueRepresentation}."); } _logger.LogDebug($"The value of DICOM item '{item}' is cryptoHashed."); }
public void GivenAString_WhenComputeHmac_CorrectHashShouldBeReturned(string input, string expectedHash) { var hashData = _function.Hash(input); Assert.Equal(expectedHash, hashData == null ? null : string.Concat(hashData.Select(b => b.ToString("x2")))); }
public void GivenAStream_WhenComputeHmac512_CorrectHashShouldBeReturned(Stream input, string expectedHash) { var hashData = CryptoHashFunction.Hash(input, new HMACSHA512(Encoding.UTF8.GetBytes(TestHashKey))); Assert.Equal(expectedHash, hashData == null ? null : string.Concat(hashData.Select(b => b.ToString("x2")))); }