मैं गोलांग में एचओटीपी (आरएफसी -4226) को लागू करने की कोशिश कर रहा हूं और मैं वैध एचओटीपी उत्पन्न करने के लिए संघर्ष कर रहा हूं। मैं इसे जावा में उत्पन्न कर सकता हूं लेकिन कुछ कारणों से गोलांग में मेरा कार्यान्वयन अलग है।एचओटीपी के लिए जावा बनाम गोलांग (आरएफसी -4226)
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));
}
और जाओ में:
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))
}
मेरा मानना है कि मुद्दा यह है कि जावा लाइन है: ByteBuffer.allocate(8).putLong(counter).array();
जाओ लाइन से एक अलग बाइट सरणी उत्पन्न करता है: यहाँ नमूने हैं binary.BigEndian.PutUint64(bs, counter)
।
जावा में, निम्न बाइट सरणी उत्पन्न होती है: 83 -116 -9 -98 115 -126 -3 -48
और गो में: 83 140 247 158 115 130 253 207
।
क्या किसी को दो पंक्तियों में अंतर पता है और मैं जावा लाइन को कैसे बंद कर सकता हूं?
वास्तव में, 'uint8 (-48)' 208 है, 207 नहीं (https://play.golang.org/p/marMBd3rwg) – JimB
@JimB आप सही हैं, अंतिम बाइट मेल नहीं खाता है। – icza
वाह - टीआईएल! व्यस्त ऑपरेशन क्या होगा? (गो संस्करण लेना और जावा संस्करण बनाना)। आपका बहुत बहुत धन्यवाद! –