/// <summary> /// Converte a Base32 string to byte array. /// </summary> /// <param name="base32"> /// Base32 formated string. /// </param> /// <param name="format"> /// Specify the Base32 encoding format. /// </param> /// <returns> /// byte array with value of Base32 encoding. /// </returns> public static byte[] Decode(string base32, Base32Format format = Base32Format.RFC4648) { if (base32.Length == 0) { return(new byte[0]); } var mapping = Base32Mapping.GetMapping(format); base32 = RemovePadding(base32, mapping.PaddingChar); byte[] result = new byte[base32.Length * 5 / 8]; int buffer = 0; int next = 0; int bitsLeft = 0; foreach (var c in base32.ToCharArray()) { if (!mapping.CharValues.ContainsKey(c)) { throw new Base32DecodingException($"Character was illigal: {c}"); } buffer <<= 5; buffer |= mapping.CharValues[c] & 31; bitsLeft += 5; if (bitsLeft >= 8) { result[next++] = (byte)(buffer >> (bitsLeft - 8)); bitsLeft -= 8; } } return(result); }
/// <summary> /// Constructs a Base32 value from a byte array. /// </summary> /// <param name="bytes"> /// Source bytes for the Base32 value. /// </param> /// <param name="format"> /// Specifies the Base32 encoding format. /// </param> public Base32(IList <byte> bytes, Base32Format format = Base32Format.RFC4648) { Bytes = bytes as ImmutableCollection <byte> ?? new ImmutableCollection <byte>(bytes); Value = Base32Encoder.Encode(bytes, format); Format = format; }
/// <summary> /// Encodes the byte array to a Base32 string. /// </summary> /// <param name="bytes"> /// Bytes for encoding. /// </param> /// <param name="format"> /// Specify the Base32 encoding format. /// </param> /// <param name="padOutput"> /// Select if end padding of the Base32 string is wanted. /// </param> /// <returns></returns> public static string Encode(IList <byte> bytes, Base32Format format = Base32Format.RFC4648, bool padOutput = true) { if (bytes == null) { throw new ArgumentNullException(nameof(bytes)); } var count = bytes.Count; if (count == 0) { return(String.Empty); } if (count >= (1 << 28)) { throw new ArgumentOutOfRangeException(nameof(bytes)); } var mapping = Base32Mapping.GetMapping(format); StringBuilder stringBuilder = new StringBuilder((count * 8 + 4) / 5); int buffer = bytes[0]; int index = 0; int pad = 0; int next = 1; int bitsLeft = 8; while (bitsLeft > 0 || next < count) { if (bitsLeft < 5) { if (next < count) { buffer <<= 8; buffer |= (bytes[next++] & 0xff); bitsLeft += 8; } else { pad = 5 - bitsLeft; buffer <<= pad; bitsLeft += pad; } } index = 31 & (buffer >> (bitsLeft - 5)); bitsLeft -= 5; stringBuilder.Append(mapping.Chars[index]); } if (padOutput) { int padding = 8 - (stringBuilder.Length % 8); if (padding > 0) { stringBuilder.Append(new string(mapping.PaddingChar, padding == 8 ? 0 : padding)); } } return(stringBuilder.ToString()); }
/// <summary> /// Parses a Base32 formated string to Base32 object. /// </summary> /// <param name="base32"> /// Base32 formated string. /// </param> /// <param name="format"> /// Specify the Base32 encoding format. /// </param> /// <returns> /// A Base32 representation of the Base32 formated string. /// </returns> public static Base32 Parse(string base32, Base32Format format = Base32Format.RFC4648) { if (base32 == null) { throw new System.ArgumentNullException(nameof(base32)); } var byteArray = Base32Decoder.Decode(base32, format); return(new Base32(byteArray, base32, format)); }
/// <summary> /// Tryies to parses a Base32 formated string to Base32 object. /// </summary> /// <param name="base32string"> /// Base32 formates string. /// </param> /// <param name="base32"> /// Return Base32 representation of the Base32 formated string. /// </param> /// <param name="format"> /// Specify the Base32 encoding format. /// </param> /// <returns> /// True if Parse of string was successful. /// </returns> public static bool TryParse(string base32string, out Base32 base32, Base32Format format = Base32Format.RFC4648) { try { base32 = Parse(base32string, format); return(true); } catch (System.Exception) { base32 = null; return(false); } }
internal static IBase32Mapping GetMapping(Base32Format base32Format) { switch (base32Format) { case Base32Format.Base32Hex: return(Base32HexMapping.Instance); case Base32Format.Crockfords: return(CrockfordsMapping.Instance); case Base32Format.RFC4648: return(Base32RFC4648Mapping.Instance); case Base32Format.ZBase32: return(ZBase32Mapping.Instance); } throw new System.NotImplementedException(); }
private Base32(IList <byte> bytes, string value, Base32Format format) { Bytes = bytes as ImmutableCollection <byte> ?? new ImmutableCollection <byte>(bytes); Value = value; Format = format; }
private Base32(SerializationInfo info, StreamingContext context) { Bytes = (ImmutableCollection <byte>)info.GetValue("B", typeof(ImmutableCollection <byte>)); Format = (Base32Format)info.GetValue("F", typeof(Base32Format)); Value = Base32Encoder.Encode(Bytes, Format); }