|
| 1 | +/+ |
| 2 | +MIT License |
| 3 | +Copyright (c) 2020 Andrea Manzini |
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +The above copyright notice and this permission notice shall be included in all |
| 11 | +copies or substantial portions of the Software. |
| 12 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | +SOFTWARE. |
| 19 | ++/ |
| 20 | + |
| 21 | +module gauthenticator; |
| 22 | + |
| 23 | +import std.stdio; |
| 24 | + |
| 25 | +class GAuthenticator |
| 26 | +{ |
| 27 | + |
| 28 | + // HMAC-based One Time Password(HOTP) |
| 29 | + public string getHOTPToken(const string secret, const ulong interval) |
| 30 | + { |
| 31 | + /* |
| 32 | + auto key=base32decode(secret); |
| 33 | + hash = HMAC-SHA1(key) |
| 34 | + offset = last nibble of hash |
| 35 | + truncatedHash := hash[offset..offset+3] //4 bytes starting at the offset |
| 36 | + Set the first bit of truncatedHash to zero //remove the most significant bit |
| 37 | + code := truncatedHash mod 1000000 |
| 38 | + pad code with 0 from the left until length of code is 6 |
| 39 | + return code */ |
| 40 | + |
| 41 | + return "000000"; |
| 42 | + } |
| 43 | + |
| 44 | + //Time-based One Time Password(TOTP) |
| 45 | + public string getTOTPToken(const string secret) |
| 46 | + { |
| 47 | + //The TOTP token is just a HOTP token seeded with every 30 seconds. |
| 48 | + import std.datetime : Clock; |
| 49 | + |
| 50 | + immutable ulong interval = Clock.currTime().toUnixTime() / 30; |
| 51 | + return getHOTPToken(secret, interval); |
| 52 | + } |
| 53 | + |
| 54 | + //RFC 4648 |
| 55 | + public int[] base32decode(const string message) |
| 56 | + { |
| 57 | + int buffer = 0; |
| 58 | + int bitsLeft = 0; |
| 59 | + int[] result; |
| 60 | + for (int i = 0; i < message.length; i++) |
| 61 | + { |
| 62 | + int ch = message[i]; |
| 63 | + writeln(ch); |
| 64 | + if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-') |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + buffer <<= 5; |
| 69 | + |
| 70 | + // Deal with commonly mistyped characters |
| 71 | + if (ch == '0') |
| 72 | + { |
| 73 | + ch = 'O'; |
| 74 | + } |
| 75 | + else if (ch == '1') |
| 76 | + { |
| 77 | + ch = 'L'; |
| 78 | + } |
| 79 | + else if (ch == '8') |
| 80 | + { |
| 81 | + ch = 'B'; |
| 82 | + } |
| 83 | + |
| 84 | + // Look up one base32 digit |
| 85 | + if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) |
| 86 | + { |
| 87 | + ch = (ch & 0x1F) - 1; |
| 88 | + } |
| 89 | + else if (ch >= '2' && ch <= '7') |
| 90 | + { |
| 91 | + ch -= '2' - 26; |
| 92 | + } |
| 93 | + |
| 94 | + buffer |= ch; |
| 95 | + bitsLeft += 5; |
| 96 | + if (bitsLeft >= 8) |
| 97 | + { |
| 98 | + result ~= (buffer >> (bitsLeft - 8)); |
| 99 | + bitsLeft -= 8; |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + return result; |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + unittest |
| 108 | + { |
| 109 | + const base32 = "gr6d 5br7 25s6 vnck v4vl hlao re======"; |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + /* |
| 114 | + unittest |
| 115 | + { |
| 116 | + auto ga = new GAuthenticator; |
| 117 | + auto secret = "dummySECRETdummy"; |
| 118 | + auto interval = ulong(50780342); |
| 119 | + auto otp = "971294"; |
| 120 | + assert(otp == ga.getHOTPToken(secret, interval)); |
| 121 | + } |
| 122 | +*/ |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +unittest |
| 127 | +{ |
| 128 | + writeln("ciao"); |
| 129 | + |
| 130 | + auto ga = new GAuthenticator(); |
| 131 | + auto expected = cast(int[]) "FOOBAR"; |
| 132 | + writeln(ga.base32decode("IZHU6QSBKIFA====")); |
| 133 | + writeln(expected); |
| 134 | +} |
0 commit comments