public static string UrlTokenEncode (byte[] input)
{
if (input == null)
throw new ArgumentNullException ("input");
if (input.Length < 1)
return String.Empty;
string base64 = Convert.ToBase64String (input);
int retlen;
if (base64 == null || (retlen = base64.Length) == 0)
return String.Empty;
// MS.NET implementation seems to process the base64
// string before returning. It replaces the chars:
//
// + with -
// / with _
//
// Then removes trailing ==, which may appear in the
// base64 string, and replaces them with a single digit
// that's the count of removed '=' characters (0 if none
// were removed)
int equalsCount = 0x30;
while (retlen > 0 && base64[retlen - 1] == '=') {
equalsCount++;
retlen--;
}
char[] chars = new char[retlen + 1];
chars[retlen] = (char)equalsCount;
for (int i = 0; i < retlen; i++) {
switch (base64[i]) {
case '+':
chars[i] = '-';
break;
case '/':
chars[i] = '_';
break;
default:
chars[i] = base64[i];
break;
}
}
return new string (chars);
}
#endif