久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用于 HOTP 的 Java 與 Golang (rfc-4226)

Java vs. Golang for HOTP (rfc-4226)(用于 HOTP 的 Java 與 Golang (rfc-4226))
本文介紹了用于 HOTP 的 Java 與 Golang (rfc-4226)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試在 Golang 中實現 HOTP (rfc-4226),并且正在努力生成有效的 HOTP.我可以在 java 中生成它,但由于某種原因,我在 Golang 中的實現有所不同.以下是示例:

I'm trying to implement HOTP (rfc-4226) in Golang and I'm struggling to generate a valid HOTP. I can generate it in java but for some reason my implementation in Golang is different. Here are the samples:

public static String constructOTP(final Long counter, final String key)
        throws NoSuchAlgorithmException, DecoderException, InvalidKeyException {
    final Mac mac = Mac.getInstance("HmacSHA512");

    final byte[] binaryKey = Hex.decodeHex(key.toCharArray());

    mac.init(new SecretKeySpec(binaryKey, "HmacSHA512"));
    final byte[] b = ByteBuffer.allocate(8).putLong(counter).array();
    byte[] computedOtp = mac.doFinal(b);

    return new String(Hex.encodeHex(computedOtp));
}

在 Go 中:

func getOTP(counter uint64, key string) string {
    str, err := hex.DecodeString(key)
    if err != nil {
        panic(err)
    }
    h := hmac.New(sha512.New, str)
    bs := make([]byte, 8)
    binary.BigEndian.PutUint64(bs, counter)
    h.Write(bs)
    return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

我認為問題在于 Java 行:ByteBuffer.allocate(8).putLong(counter).array(); 生成的字節數組與 Go 行不同:binary.BigEndian.PutUint64(bs, counter).

I believe the issue is that the Java line: ByteBuffer.allocate(8).putLong(counter).array(); generates a different byte array than the Go line: binary.BigEndian.PutUint64(bs, counter).

在 Java 中,生成以下字節數組:83 -116 -9 -98 115 -126 -3 -48 而在 Go 中:83 140 247 158 115 130 253 207.

In Java, the following byte array is generated: 83 -116 -9 -98 115 -126 -3 -48 and in Go: 83 140 247 158 115 130 253 207.

有人知道這兩條線的區別以及如何移植 java 線嗎?

Does anybody know the difference in the two lines and how I can port the java line to go?

推薦答案

Java中的byte類型是有符號的,它的范圍是-128..127,而在 Go 中,byteuint8 的別名,范圍為 0..255.所以如果你想比較結果,你必須將Java的負值移動256(添加256).

The byte type in Java is signed, it has a range of -128..127, while in Go byte is an alias of uint8 and has a range of 0..255. So if you want to compare the results, you have to shift negative Java values by 256 (add 256).

提示:要以無符號方式顯示 Java byte 值,請使用:byteValue &0xff 使用 byte 的 8 位作為 int 中的最低 8 位將其轉換為 int.或者更好:以十六進制形式顯示兩個結果,這樣您就不必關心符號...

Tip: To display a Java byte value in an unsigned fashion, use: byteValue & 0xff which converts it to int using the 8 bits of the byte as the lowest 8 bits in the int. Or better: display both results in hex form so you don't have to care about sign-ness...

將 256 添加到負 Java 字節值,輸出幾乎與 Go 相同:最??后一個字節減 1:

Adding 256 to your negative Java byte values, the output is almost identical to Go's: the last byte is off by 1:

javabytes := []int{83, -116, -9, -98, 115, -126, -3, -48}
for i, b := range javabytes {
    if b < 0 {
        javabytes[i] += 256
    }
}
fmt.Println(javabytes)

輸出是:

[83 140 247 158 115 130 253 208]

因此,Java 數組的最后一個字節是 208,而 Go 數組的最后一個字節是 207.我猜您的 counter 會在您未發布的代碼中的其他地方增加一次.

So the last byte of your Java array is 208 while Go's is 207. I'm guessing your counter is incremented once somewhere else in your code which you haven't posted.

不同的是,在 Java 中返回的是十六進制編碼的結果,而在 Go 中返回的是 Base64 編碼的結果(它們是兩種不同的編碼,給出完全不同的結果).正如您所確認的,在返回 hex.EncodeToString(h.Sum(nil)) 的 Go 中,結果匹配.

What differs is that in Java you return the hex encoded result while in Go you return the Base64 encoded result (they are 2 different encodings giving entirely different results). As you confirmed, in Go returning hex.EncodeToString(h.Sum(nil)) the results match.

提示 #2:要以簽名方式顯示 Go 的字節,只需將它們轉換為 int8(已簽名),如下所示:

Tip #2: To display Go's bytes in a signed fashion, simply convert them to int8 (which is signed) like this:

gobytes := []byte{83, 140, 247, 158, 115, 130, 253, 207}
for _, b := range gobytes {
    fmt.Print(int8(b), " ")
}

這個輸出:

83 -116 -9 -98 115 -126 -3 -49 

這篇關于用于 HOTP 的 Java 與 Golang (rfc-4226)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 懂色一区二区三区免费观看 | 在线不卡一区 | 亚洲成人网在线播放 | 成人h免费观看视频 | 精品日韩 | 亚洲视频二区 | 成人免费精品 | 久久99视频免费观看 | 亚洲一区二区三区视频免费观看 | 国产精品久久久久aaaa九色 | 九九热在线免费视频 | 国产色网 | 新91视频网 | 超碰成人免费 | 亚洲国产精品成人久久久 | 亚洲在线视频 | 啪啪免费网站 | 成人自拍视频网站 | 久久久久久久久久久久久91 | 欧美激情在线观看一区二区三区 | 精品www | 亚洲日韩欧美一区二区在线 | 欧美中文视频 | 亚洲欧美精品 | 性色视频 | 日日干夜夜操 | 久久伊人精品一区二区三区 | 亚洲精品乱码久久久久v最新版 | 一级欧美视频 | 久久精品国产a三级三级三级 | 国产精品亚洲成在人线 | 伊人网影院 | 妖精视频一区二区三区 | 波多野结衣一二三区 | 午夜视频一区 | 国产在线1区 | 亚洲成人三区 | 亚洲国产成人精品女人久久久野战 | 久久高清 | 激情亚洲| 精品欧美一区二区精品久久久 |