| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Security.Cryptography; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel |
| | | 9 | | { |
| | | 10 | | internal sealed class HashStream : Stream |
| | | 11 | | { |
| | | 12 | | private long _length; |
| | | 13 | | private bool _disposed; |
| | | 14 | | private bool _hashNeedsReset; |
| | | 15 | | private MemoryStream _logStream; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Constructor for HashStream. The HashAlgorithm instance is owned by the caller. |
| | | 19 | | /// </summary> |
| | 23 | 20 | | public HashStream(HashAlgorithm hash) |
| | | 21 | | { |
| | 23 | 22 | | if (hash == null) |
| | | 23 | | { |
| | 0 | 24 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(hash)); |
| | | 25 | | } |
| | | 26 | | |
| | 23 | 27 | | Reset(hash); |
| | 23 | 28 | | } |
| | | 29 | | |
| | | 30 | | public override bool CanRead |
| | | 31 | | { |
| | 0 | 32 | | get { return false; } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public override bool CanWrite |
| | | 36 | | { |
| | 0 | 37 | | get { return true; } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public override bool CanSeek |
| | | 41 | | { |
| | 0 | 42 | | get { return false; } |
| | | 43 | | } |
| | | 44 | | |
| | 92 | 45 | | public HashAlgorithm Hash { get; private set; } |
| | | 46 | | |
| | | 47 | | public override long Length |
| | | 48 | | { |
| | 0 | 49 | | get { return _length; } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public override long Position |
| | | 53 | | { |
| | 0 | 54 | | get { return _length; } |
| | | 55 | | set |
| | | 56 | | { |
| | 0 | 57 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public override void Flush() |
| | | 62 | | { |
| | 23 | 63 | | } |
| | | 64 | | |
| | | 65 | | public void FlushHash() |
| | | 66 | | { |
| | 0 | 67 | | FlushHash(null); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public void FlushHash(MemoryStream preCanonicalBytes) |
| | | 71 | | { |
| | 23 | 72 | | Hash.TransformFinalBlock(CryptoHelper.EmptyBuffer, 0, 0); |
| | | 73 | | //TODO logs Pii data |
| | | 74 | | //if (DigestTraceRecordHelper.ShouldTraceDigest) |
| | | 75 | | // DigestTraceRecordHelper.TraceDigest(this.logStream, this.hash); |
| | 23 | 76 | | } |
| | | 77 | | |
| | | 78 | | public byte[] FlushHashAndGetValue() |
| | | 79 | | { |
| | 23 | 80 | | return FlushHashAndGetValue(null); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public byte[] FlushHashAndGetValue(MemoryStream preCanonicalBytes) |
| | | 84 | | { |
| | 23 | 85 | | FlushHash(preCanonicalBytes); |
| | 23 | 86 | | return Hash.Hash; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public void Reset() |
| | | 95 | | { |
| | 0 | 96 | | if (_hashNeedsReset) |
| | | 97 | | { |
| | 0 | 98 | | Hash.Initialize(); |
| | 0 | 99 | | _hashNeedsReset = false; |
| | | 100 | | } |
| | 0 | 101 | | _length = 0; |
| | | 102 | | |
| | | 103 | | // if (DigestTraceRecordHelper.ShouldTraceDigest) |
| | | 104 | | // this.logStream = new MemoryStream(); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | public void Reset(HashAlgorithm hash) |
| | | 108 | | { |
| | 23 | 109 | | Hash = hash; |
| | 23 | 110 | | _hashNeedsReset = false; |
| | 23 | 111 | | _length = 0; |
| | | 112 | | |
| | | 113 | | // if (DigestTraceRecordHelper.ShouldTraceDigest) |
| | | 114 | | // this.logStream = new MemoryStream(); |
| | 23 | 115 | | } |
| | | 116 | | |
| | | 117 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 118 | | { |
| | 23 | 119 | | Hash.TransformBlock(buffer, offset, count, buffer, offset); |
| | 23 | 120 | | _length += count; |
| | 23 | 121 | | _hashNeedsReset = true; |
| | | 122 | | |
| | | 123 | | // if (DigestTraceRecordHelper.ShouldTraceDigest) |
| | | 124 | | // this.logStream.Write(buffer, offset, count); |
| | 23 | 125 | | } |
| | | 126 | | |
| | | 127 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 128 | | { |
| | 0 | 129 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | public override void SetLength(long length) |
| | | 133 | | { |
| | 0 | 134 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | #region IDisposable members |
| | | 138 | | |
| | | 139 | | protected override void Dispose(bool disposing) |
| | | 140 | | { |
| | 0 | 141 | | base.Dispose(disposing); |
| | | 142 | | |
| | 0 | 143 | | if (_disposed) |
| | | 144 | | { |
| | 0 | 145 | | return; |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | if (disposing) |
| | | 149 | | { |
| | | 150 | | // |
| | | 151 | | // Free all of our managed resources |
| | | 152 | | // |
| | | 153 | | |
| | 0 | 154 | | if (_logStream != null) |
| | | 155 | | { |
| | 0 | 156 | | _logStream.Dispose(); |
| | 0 | 157 | | _logStream = null; |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | // Free native resources, if any. |
| | | 162 | | |
| | 0 | 163 | | _disposed = true; |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | #endregion |
| | | 167 | | } |
| | | 168 | | } |