Skip to content

Commit 0a8e07a

Browse files
committed
Add ComputeSha256 ext method
1 parent f047c5c commit 0a8e07a

2 files changed

Lines changed: 91 additions & 77 deletions

File tree

ServiceStack.Text/src/ServiceStack.Text/StreamExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,5 +782,5 @@ public static async Task WritePartialToAsync(this Stream fromStream, Stream toSt
782782
}
783783

784784
SharedPools.AsyncByteArray.Free(buf);
785-
}
785+
}
786786
}

ServiceStack/src/ServiceStack.Client/StreamExt.cs

Lines changed: 90 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,97 @@
66
using System.IO;
77
using System.Text;
88

9-
namespace ServiceStack
9+
namespace ServiceStack;
10+
11+
public static class StreamExt
1012
{
11-
public static class StreamExt
13+
/// <summary>
14+
/// Compresses the specified text using the default compression method: Deflate
15+
/// </summary>
16+
/// <param name="text">The text.</param>
17+
/// <param name="compressionType">Type of the compression.</param>
18+
/// <param name="encoding"></param>
19+
/// <returns></returns>
20+
public static byte[] Compress(this string text, string compressionType, Encoding? encoding=null) =>
21+
StreamCompressors.GetRequired(compressionType).Compress(text, encoding);
22+
23+
public static Stream CompressStream(this Stream stream, string compressionType) =>
24+
StreamCompressors.GetRequired(compressionType).Compress(stream);
25+
26+
/// <summary>
27+
/// Compresses the specified text using the default compression method: Deflate
28+
/// </summary>
29+
public static byte[] CompressBytes(this byte[] bytes, string compressionType) =>
30+
StreamCompressors.GetRequired(compressionType).Compress(bytes);
31+
32+
/// <summary>
33+
/// Decompresses the specified gz buffer using the default compression method: Inflate
34+
/// </summary>
35+
/// <param name="gzBuffer">The gz buffer.</param>
36+
/// <param name="compressionType">Type of the compression.</param>
37+
/// <returns></returns>
38+
public static string Decompress(this byte[] gzBuffer, string compressionType) =>
39+
StreamCompressors.GetRequired(compressionType).Decompress(gzBuffer);
40+
41+
/// <summary>
42+
/// Decompresses the specified gz buffer using inflate or gzip method
43+
/// </summary>
44+
/// <param name="gzStream">Compressed stream</param>
45+
/// <param name="compressionType">Type of the compression. Can be "gzip" or "deflate"</param>
46+
/// <returns>Decompressed stream</returns>
47+
public static Stream Decompress(this Stream gzStream, string compressionType) =>
48+
!string.IsNullOrEmpty(compressionType)
49+
? StreamCompressors.GetRequired(compressionType).Decompress(gzStream)
50+
: gzStream;
51+
52+
/// <summary>
53+
/// Decompresses the specified gz buffer using the default compression method: Inflate
54+
/// </summary>
55+
public static byte[] DecompressBytes(this byte[] gzBuffer, string compressionType) =>
56+
StreamCompressors.GetRequired(compressionType).DecompressBytes(gzBuffer);
57+
58+
public static byte[] Deflate(this string text) => DeflateCompressor.Instance.Compress(text);
59+
60+
public static string Inflate(this byte[] gzBuffer) => DeflateCompressor.Instance.Decompress(gzBuffer);
61+
62+
public static byte[] GZip(this string text) => GZipCompressor.Instance.Compress(text);
63+
64+
public static string GUnzip(this byte[] gzBuffer) => GZipCompressor.Instance.Decompress(gzBuffer);
65+
66+
public static string ToUtf8String(this Stream stream)
1267
{
13-
/// <summary>
14-
/// Compresses the specified text using the default compression method: Deflate
15-
/// </summary>
16-
/// <param name="text">The text.</param>
17-
/// <param name="compressionType">Type of the compression.</param>
18-
/// <param name="encoding"></param>
19-
/// <returns></returns>
20-
public static byte[] Compress(this string text, string compressionType, Encoding? encoding=null) =>
21-
StreamCompressors.GetRequired(compressionType).Compress(text, encoding);
22-
23-
public static Stream CompressStream(this Stream stream, string compressionType) =>
24-
StreamCompressors.GetRequired(compressionType).Compress(stream);
25-
26-
/// <summary>
27-
/// Compresses the specified text using the default compression method: Deflate
28-
/// </summary>
29-
public static byte[] CompressBytes(this byte[] bytes, string compressionType) =>
30-
StreamCompressors.GetRequired(compressionType).Compress(bytes);
31-
32-
/// <summary>
33-
/// Decompresses the specified gz buffer using the default compression method: Inflate
34-
/// </summary>
35-
/// <param name="gzBuffer">The gz buffer.</param>
36-
/// <param name="compressionType">Type of the compression.</param>
37-
/// <returns></returns>
38-
public static string Decompress(this byte[] gzBuffer, string compressionType) =>
39-
StreamCompressors.GetRequired(compressionType).Decompress(gzBuffer);
40-
41-
/// <summary>
42-
/// Decompresses the specified gz buffer using inflate or gzip method
43-
/// </summary>
44-
/// <param name="gzStream">Compressed stream</param>
45-
/// <param name="compressionType">Type of the compression. Can be "gzip" or "deflate"</param>
46-
/// <returns>Decompressed stream</returns>
47-
public static Stream Decompress(this Stream gzStream, string compressionType) =>
48-
!string.IsNullOrEmpty(compressionType)
49-
? StreamCompressors.GetRequired(compressionType).Decompress(gzStream)
50-
: gzStream;
51-
52-
/// <summary>
53-
/// Decompresses the specified gz buffer using the default compression method: Inflate
54-
/// </summary>
55-
public static byte[] DecompressBytes(this byte[] gzBuffer, string compressionType) =>
56-
StreamCompressors.GetRequired(compressionType).DecompressBytes(gzBuffer);
57-
58-
public static byte[] Deflate(this string text) => DeflateCompressor.Instance.Compress(text);
59-
60-
public static string Inflate(this byte[] gzBuffer) => DeflateCompressor.Instance.Decompress(gzBuffer);
61-
62-
public static byte[] GZip(this string text) => GZipCompressor.Instance.Compress(text);
63-
64-
public static string GUnzip(this byte[] gzBuffer) => GZipCompressor.Instance.Decompress(gzBuffer);
65-
66-
public static string ToUtf8String(this Stream stream)
67-
{
68-
if (stream == null)
69-
throw new ArgumentNullException(nameof(stream));
70-
71-
return stream.ReadToEnd();
72-
}
73-
74-
public static byte[] ToBytes(this Stream stream)
75-
{
76-
if (stream == null)
77-
throw new ArgumentNullException(nameof(stream));
78-
79-
return stream.ReadFully();
80-
}
81-
82-
public static void Write(this Stream stream, string text)
83-
{
84-
var bytes = Encoding.UTF8.GetBytes(text);
85-
stream.Write(bytes, 0, bytes.Length);
86-
}
68+
if (stream == null)
69+
throw new ArgumentNullException(nameof(stream));
70+
71+
return stream.ReadToEnd();
8772
}
73+
74+
public static byte[] ToBytes(this Stream stream)
75+
{
76+
if (stream == null)
77+
throw new ArgumentNullException(nameof(stream));
78+
79+
return stream.ReadFully();
80+
}
81+
82+
public static void Write(this Stream stream, string text)
83+
{
84+
var bytes = Encoding.UTF8.GetBytes(text);
85+
stream.Write(bytes, 0, bytes.Length);
86+
}
87+
88+
public static string ComputeSha256(this byte[] data)
89+
{
90+
using var sha256 = System.Security.Cryptography.SHA256.Create();
91+
byte[] hashBytes = sha256.ComputeHash(data);
92+
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
93+
}
94+
95+
public static string ComputeSha256(this Stream stream)
96+
{
97+
stream.Position = 0;
98+
using var sha256 = System.Security.Cryptography.SHA256.Create();
99+
byte[] hashBytes = sha256.ComputeHash(stream);
100+
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
101+
}
88102
}

0 commit comments

Comments
 (0)