This commit is contained in:
denis
2026-06-02 11:58:08 -04:00
committed by GitHub
parent 3051825098
commit e53c8d02af
16 changed files with 99 additions and 16 deletions
@@ -0,0 +1,32 @@
package de.caydenno1.xacrypto.hash.sha256;
import de.caydenno1.xacrypto.misc.XACryptoException;
import java.nio.charset.StandardCharsets;
import static de.caydenno1.xacrypto.hash.sha256.Hex.hash;
public class HMAC {
private HMAC(){}
public static byte[] hmac(byte[] key, byte[] mesg) throws XACryptoException {
byte[] k = (key.length > 64) ? hash(key) : key;
byte[] i = new byte[64];
byte[] o = new byte[64];
for (int l=0;l<64;l++) {
byte k_ = (l < k.length) ? k[l] : 0;
i[l] = (byte)(k_^0x36);
o[l] = (byte)(k_^0x5c);
}
byte[] in = new Digest().upd(i).upd(mesg).digest();
return new Digest().upd(o).upd(in).digest();
}
public static byte[] hmac(String key, String mesg) throws XACryptoException {
return hmac(key.getBytes(StandardCharsets.UTF_8),
mesg.getBytes(StandardCharsets.UTF_8));
}
}