forked from akkadotnet/akka.net
/
Base64Encoding.cs
86 lines (78 loc) · 2.68 KB
/
Base64Encoding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//-----------------------------------------------------------------------
// <copyright file="Base64Encoding.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Text;
namespace Akka.Util
{
/// <summary>
/// TBD
/// </summary>
public static class Base64Encoding
{
/// <summary>
/// TBD
/// </summary>
public const string Base64Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+~";
/// <summary>
/// TBD
/// </summary>
/// <param name="value">TBD</param>
/// <returns>TBD</returns>
public static string Base64Encode(this long value)
{
return Base64Encode(value, "");
}
internal static string Base64Encode(this long value, string prefix)
{
// 11 is the number of characters it takes to represent long.MaxValue
// so we will never need a larger size for encoding longs
Span<char> sb = stackalloc char[11 + prefix?.Length ?? 0];
var spanIndex = 0;
if (!string.IsNullOrWhiteSpace(prefix) && prefix.Length > 0)
{
prefix.AsSpan().CopyTo(sb);
spanIndex = prefix.Length;
}
var next = value;
do
{
var index = (int)(next & 63);
sb[spanIndex++] = Base64Chars[index];
next = next >> 6;
} while (next != 0);
return sb.Slice(0, spanIndex).ToString();
}
/// <summary>
/// TBD
/// </summary>
/// <param name="value">TBD</param>
/// <param name="sb">TBD</param>
/// <returns>TBD</returns>
[Obsolete("Do not use. Pass in prefix as a string instead.")]
public static StringBuilder Base64Encode(this long value, StringBuilder sb)
{
var next = value;
do
{
var index = (int)(next & 63);
sb.Append(Base64Chars[index]);
next = next >> 6;
} while (next != 0);
return sb;
}
/// <summary>
/// TBD
/// </summary>
/// <param name="s">TBD</param>
/// <returns>TBD</returns>
public static string Base64Encode(this string s)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(s);
return System.Convert.ToBase64String(bytes);
}
}
}