|
| 1 | +/* |
| 2 | +Credits to "Shane" from SO answer here: |
| 3 | +http://stackoverflow.com/a/7135008/1090359 |
| 4 | +*/ |
| 5 | + |
| 6 | +using System; |
| 7 | + |
| 8 | +namespace OtpNet |
| 9 | +{ |
| 10 | + public class Base32Encoding |
| 11 | + { |
| 12 | + public static byte[] ToBytes(string input) |
| 13 | + { |
| 14 | + if(string.IsNullOrEmpty(input)) |
| 15 | + { |
| 16 | + throw new ArgumentNullException("input"); |
| 17 | + } |
| 18 | + |
| 19 | + input = input.TrimEnd('='); //remove padding characters |
| 20 | + int byteCount = input.Length * 5 / 8; //this must be TRUNCATED |
| 21 | + byte[] returnArray = new byte[byteCount]; |
| 22 | + |
| 23 | + byte curByte = 0, bitsRemaining = 8; |
| 24 | + int mask = 0, arrayIndex = 0; |
| 25 | + |
| 26 | + foreach(char c in input) |
| 27 | + { |
| 28 | + int cValue = CharToValue(c); |
| 29 | + |
| 30 | + if(bitsRemaining > 5) |
| 31 | + { |
| 32 | + mask = cValue << (bitsRemaining - 5); |
| 33 | + curByte = (byte)(curByte | mask); |
| 34 | + bitsRemaining -= 5; |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + mask = cValue >> (5 - bitsRemaining); |
| 39 | + curByte = (byte)(curByte | mask); |
| 40 | + returnArray[arrayIndex++] = curByte; |
| 41 | + curByte = (byte)(cValue << (3 + bitsRemaining)); |
| 42 | + bitsRemaining += 3; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + //if we didn't end with a full byte |
| 47 | + if(arrayIndex != byteCount) |
| 48 | + { |
| 49 | + returnArray[arrayIndex] = curByte; |
| 50 | + } |
| 51 | + |
| 52 | + return returnArray; |
| 53 | + } |
| 54 | + |
| 55 | + public static string ToString(byte[] input) |
| 56 | + { |
| 57 | + if(input == null || input.Length == 0) |
| 58 | + { |
| 59 | + throw new ArgumentNullException("input"); |
| 60 | + } |
| 61 | + |
| 62 | + int charCount = (int)Math.Ceiling(input.Length / 5d) * 8; |
| 63 | + char[] returnArray = new char[charCount]; |
| 64 | + |
| 65 | + byte nextChar = 0, bitsRemaining = 5; |
| 66 | + int arrayIndex = 0; |
| 67 | + |
| 68 | + foreach(byte b in input) |
| 69 | + { |
| 70 | + nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining))); |
| 71 | + returnArray[arrayIndex++] = ValueToChar(nextChar); |
| 72 | + |
| 73 | + if(bitsRemaining < 4) |
| 74 | + { |
| 75 | + nextChar = (byte)((b >> (3 - bitsRemaining)) & 31); |
| 76 | + returnArray[arrayIndex++] = ValueToChar(nextChar); |
| 77 | + bitsRemaining += 5; |
| 78 | + } |
| 79 | + |
| 80 | + bitsRemaining -= 3; |
| 81 | + nextChar = (byte)((b << bitsRemaining) & 31); |
| 82 | + } |
| 83 | + |
| 84 | + //if we didn't end with a full char |
| 85 | + if(arrayIndex != charCount) |
| 86 | + { |
| 87 | + returnArray[arrayIndex++] = ValueToChar(nextChar); |
| 88 | + while(arrayIndex != charCount) returnArray[arrayIndex++] = '='; //padding |
| 89 | + } |
| 90 | + |
| 91 | + return new string(returnArray); |
| 92 | + } |
| 93 | + |
| 94 | + private static int CharToValue(char c) |
| 95 | + { |
| 96 | + int value = (int)c; |
| 97 | + |
| 98 | + //65-90 == uppercase letters |
| 99 | + if(value < 91 && value > 64) |
| 100 | + { |
| 101 | + return value - 65; |
| 102 | + } |
| 103 | + //50-55 == numbers 2-7 |
| 104 | + if(value < 56 && value > 49) |
| 105 | + { |
| 106 | + return value - 24; |
| 107 | + } |
| 108 | + //97-122 == lowercase letters |
| 109 | + if(value < 123 && value > 96) |
| 110 | + { |
| 111 | + return value - 97; |
| 112 | + } |
| 113 | + |
| 114 | + throw new ArgumentException("Character is not a Base32 character.", "c"); |
| 115 | + } |
| 116 | + |
| 117 | + private static char ValueToChar(byte b) |
| 118 | + { |
| 119 | + if(b < 26) |
| 120 | + { |
| 121 | + return (char)(b + 65); |
| 122 | + } |
| 123 | + |
| 124 | + if(b < 32) |
| 125 | + { |
| 126 | + return (char)(b + 24); |
| 127 | + } |
| 128 | + |
| 129 | + throw new ArgumentException("Byte is not a value Base32 value.", "b"); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | +} |
0 commit comments