現在為了安全Google二次驗證使用越來越平凡了,所以我們自己做的一些產品中,也會用到Google Authenticator。
介紹
Google Authenticator采用的算法是TOTP(Time-Based One-Time Password基于時間的一次性密碼),其核心內容包括以下三點:
- 一個共享密鑰(一個比特序列);
- 當前時間輸入;
- 一個簽署函數。
1. 共享密鑰
由服務器生成一個16位純字母的字符串,用于在手機端上建立賬戶,可以手動輸入,也可以生成二位維在手機上掃描
令牌的二維碼的內容是一個URL:
otpauth://totp/賬號名?secret=xxxxxxxxxxxxxxxx&issuer=組織名
2. 時間
服務器和手機端使用各自的時間,只要時間都是準確的,不無需與服務器做任何通信。每30秒切換一次,所以時間用的當前時間戳/30。是但為了避免時間上的誤差,服務器驗證的時候可以取當前一次和后面一次,一共三次判斷。
3. 簽署函數
簽署所使用的方法是HMAC-SHA1(哈希運算消息認證碼),以一個密鑰和一個消息為輸入,生成一個消息摘要作為輸出。用共享密鑰做為secret,時間戳/30做為輸入值來生成20字節的SHA1值,
4. 生成6位數密碼
先把SHA1的最后4個比特數用來做索引,然后用另外的4個字節進行索引。然后將它轉化為標準的32bit無符號整數,最后再進行7位數(1百萬)取整,就可得到6位數字了
實現
1. 手機客戶端直接下載Google Authenticator
2. 服務器驗證代碼如下:
import ("crypto/hmac""crypto/sha1""encoding/base32""strings""time""util/log"
)func toBytes(value int64) []byte {var result []bytemask := int64(0xFF)shifts := [8]uint16{56, 48, 40, 32, 24, 16, 8, 0}for _, shift := range shifts {result = append(result, byte((value>>shift)&mask))}return result
}func toUint32(bytes []byte) uint32 {return (uint32(bytes[0]) << 24) + (uint32(bytes[1]) << 16) +(uint32(bytes[2]) << 8) + uint32(bytes[3])
}func oneTimePassword(key []byte, value []byte) uint32 {// sign the value using HMAC-SHA1hmacSha1 := hmac.New(sha1.New, key)hmacSha1.Write(value)hash := hmacSha1.Sum(nil)// We're going to use a subset of the generated hash.// Using the last nibble (half-byte) to choose the index to start from.// This number is always appropriate as it's maximum decimal 15, the hash will// have the maximum index 19 (20 bytes of SHA1) and we need 4 bytes.offset := hash[len(hash)-1] & 0x0F// get a 32-bit (4-byte) chunk from the hash starting at offsethashParts := hash[offset : offset+4]// ignore the most significant bit as per RFC 4226hashParts[0] = hashParts[0] & 0x7Fnumber := toUint32(hashParts)// size to 6 digits// one million is the first number with 7 digits so the remainder// of the division will always return < 7 digitspwd := number % 1000000return pwd
}// getCode 獲取驗證碼
func getCode(secretKey string, epochSeconds int64) (code int32) {secretKeyUpper := strings.ToUpper(secretKey)key, err := base32.StdEncoding.DecodeString(secretKeyUpper)if err != nil {log.Error(err)return}// generate a one-time password using the time at 30-second intervalscode = int32(oneTimePassword(key, toBytes(epochSeconds/30)))return
}// 驗證,傳入驗證key和code代碼,返回驗證是否成功
func CheckCode(secretKey string, code int32) bool {// 當前google值epochSeconds := time.Now().Unix()if getCode(secretKey, epochSeconds ) == code {return true}// 前30秒google值if getCode(secretKey, epochSeconds -30) == code {return true}// 后30秒google值if getCode(secretKey, epochSeconds +30) == code {return true}return false
}