tags:

views:

2453

answers:

3
+3  Q: 

Base32 Decoding

I have a base32 string which I need to convert to a byte array. And I'm having trouble finding a conversion method in the .net framework. I can find methods for base64 but not for base32.

Convert.FromBase64String – something like this for base32 would be perfect.

Is there such a method in the framework or do I have to roll my own?

+5  A: 

Check this FromBase32String implementation for .NET found here.

CMS
+1 Nice ... he'll probably have to change the encoding digits to the "standard" ones, but that's not bad.
Daniel LeCheminant
+1  A: 

Here's my quickly jotted solution. It only works on strings that are a multiple of 8 base32 characters. Does work, though.

public class Base32
{
  /// <summary>
  /// Decode a Base32 string
  /// This will only work on a multiple of 40 bits (5 bytes)
  /// http://www.garykessler.net/library/base64.html
  /// </summary>
  public static byte[] Decode(string Base32String)
  {
    // Ignore hyphens
    string str = Base32String.Replace("-", "");

    // Convert it to bits
    List<byte> bits = new List<byte>();
    foreach (char c in str)
    {
      int i = CharToValue(c);
      bits.Add((byte)((i & 16) > 0 ? 1 : 0));
      bits.Add((byte)((i & 8) > 0 ? 1 : 0));
      bits.Add((byte)((i & 4) > 0 ? 1 : 0));
      bits.Add((byte)((i & 2) > 0 ? 1 : 0));
      bits.Add((byte)((i & 1) > 0 ? 1 : 0));
    }

    // Convert bits into bytes
    List<byte> bytes = new List<byte>();
    for (int i = 0; i < bits.Count; i += 8)
    {
      bytes.Add((byte)(
        (bits[i + 0] << 7) +
        (bits[i + 1] << 6) +
        (bits[i + 2] << 5) +
        (bits[i + 3] << 4) +
        (bits[i + 4] << 3) +
        (bits[i + 5] << 2) +
        (bits[i + 6] << 1) +
        (bits[i + 7] << 0)));
    }

    return bytes.ToArray();
  }

  static int CharToValue(char c)
  {
    char cl = char.ToLower(c);
    if (cl == 'a') return 0;
    if (cl == 'b') return 1;
    if (cl == 'c') return 2;
    if (cl == 'd') return 3;
    if (cl == 'e') return 4;
    if (cl == 'f') return 5;
    if (cl == 'g') return 6;
    if (cl == 'h') return 7;
    if (cl == 'i') return 8;
    if (cl == 'j') return 9;
    if (cl == 'k') return 10;
    if (cl == 'l') return 11;
    if (cl == 'm') return 12;
    if (cl == 'n') return 13;
    if (cl == 'o') return 14;
    if (cl == 'p') return 15;
    if (cl == 'q') return 16;
    if (cl == 'r') return 17;
    if (cl == 's') return 18;
    if (cl == 't') return 19;
    if (cl == 'u') return 20;
    if (cl == 'v') return 21;
    if (cl == 'w') return 22;
    if (cl == 'x') return 23;
    if (cl == 'y') return 24;
    if (cl == 'z') return 25;
    if (cl == '2') return 26;
    if (cl == '3') return 27;
    if (cl == '4') return 28;
    if (cl == '5') return 29;
    if (cl == '6') return 30;
    if (cl == '7') return 31;
    throw new Exception("Not a base32 string");
  }
}
Chris
CharToValue can be made much simpler using casting and subtraction ;-] For example, 'b' - 'a' = 1
Daniel LeCheminant
CharToValue is a WTF! Also whats the use of a decoder without an encoder?
leppie
Good point daniel, i'm not sure what i was smoking at the time!
Chris
+1  A: 

I've written some flexible standards based implementations of various Base32 and Base64 encoding/decoding methods. Notably: base64url (per rfc4648) and its base32 equivalent.

By default the Base32Url class encodes with only the characters A to Z and 2 to 7. No hyphens, underscores, pluses, slashes or equals are used, making it usable as a URL token in almost all circumstances. Base32Url also supports custom alphabets, case sensitivity/insensitivity, padding/no-padding etc.

This is posted up on code project.

Mhano