网上论坛上看到的域外某大神实现的google 二次验证动态码。这个之间在其他应用上我也有应用过,包括本站在好多年前也已用上该技术,不过通过golang来实现一直没了解,刚好手头的一个小工具,想加上该功能,就先找到了如下代码:

 1package main
 2import (
 3    "crypto/hmac"
 4    "crypto/sha1"
 5    "encoding/base32"
 6    "fmt"
 7    "os"
 8    "strings"
 9    "time"
10)
11func toBytes(value int64) []byte {
12    var result []byte
13    mask := int64(0xFF)
14    shifts := [8]uint16{56, 48, 40, 32, 24, 16, 8, 0}
15    for _, shift := range shifts {
16        result = append(result, byte((value>>shift)&mask))
17    }
18    return result
19}
20func toUint32(bytes []byte) uint32 {
21    return (uint32(bytes[0]) << 24) + (uint32(bytes[1]) << 16) +
22        (uint32(bytes[2]) << 8) + uint32(bytes[3])
23}
24func oneTimePassword(key []byte, value []byte) uint32 {
25    // sign the value using HMAC-SHA1
26    hmacSha1 := hmac.New(sha1.New, key)
27    hmacSha1.Write(value)
28    hash := hmacSha1.Sum(nil)
29    // We're going to use a subset of the generated hash.
30    // Using the last nibble (half-byte) to choose the index to start from.
31    // This number is always appropriate as it's maximum decimal 15, the hash will
32    // have the maximum index 19 (20 bytes of SHA1) and we need 4 bytes.
33    offset := hash[len(hash)-1] & 0x0F
34    // get a 32-bit (4-byte) chunk from the hash starting at offset
35    hashParts := hash[offset : offset+4]
36    // ignore the most significant bit as per RFC 4226
37    hashParts[0] = hashParts[0] & 0x7F
38    number := toUint32(hashParts)
39    // size to 6 digits
40    // one million is the first number with 7 digits so the remainder
41    // of the division will always return < 7 digits
42    pwd := number % 1000000
43    return pwd
44}
45// all []byte in this program are treated as Big Endian
46func main() {
47    if len(os.Args) < 2 {
48        fmt.Fprintln(os.Stderr, "must specify key to use")
49        os.Exit(1)
50    }
51    input := os.Args[1]
52    // decode the key from the first argument
53    inputNoSpaces := strings.Replace(input, " ", "", -1)
54    inputNoSpacesUpper := strings.ToUpper(inputNoSpaces)
55    key, err := base32.StdEncoding.DecodeString(inputNoSpacesUpper)
56    if err != nil {
57        fmt.Fprintln(os.Stderr, err.Error())
58        os.Exit(1)
59    }
60    // generate a one-time password using the time at 30-second intervals
61    epochSeconds := time.Now().Unix()
62    pwd := oneTimePassword(key, toBytes(epochSeconds/30))
63    secondsRemaining := 30 - (epochSeconds % 30)
64    fmt.Printf("%06d (%d second(s) remaining)\n", pwd, secondsRemaining)
65}

上面的代码虽然已经很经典了,但在使用时,可能还需要改动下oneTimePassword函数,因为其返回的默认是一个 uint32 类型的值。经常我们通过屏幕输入获取到的是字符串值,这就需要转化为同样类型的值才容易进行比较和调用。修改方式如下:

func oneTimePassword(key []byte, value []byte) string {
hmacSha1 := hmac.New(sha1.New, key)
hmacSha1.Write(value)
hash := hmacSha1.Sum(nil)
offset := hash[len(hash)-1] & 0x0F
hashParts := hash[offset : offset+4]
hashParts[0] = hashParts[0] & 0x7F
number := toUint32(hashParts)
pwd := number % 1000000
return strconv.Itoa(int(pwd))

具体效果如下:

google-auth