Package jenkins.security
Class HMACConfidentialKey
- java.lang.Object
-
- jenkins.security.ConfidentialKey
-
- jenkins.security.HMACConfidentialKey
-
public class HMACConfidentialKey extends ConfidentialKey
ConfidentialKey
that's used for creating a token by hashing some information with secret (such ashash(msg|secret)
).This provides more secure version of it by using HMAC. See this blog post for background. This implementation also never leaks the secret value to outside, so it makes it impossible for the careless caller to misuse the key (thus protecting ourselves from our own stupidity!)
- Since:
- 1.498
- Author:
- Kohsuke Kawaguchi
-
-
Constructor Summary
Constructors Constructor Description HMACConfidentialKey(Class owner, String shortName)
HMACConfidentialKey(Class owner, String shortName, int length)
Calls intoHMACConfidentialKey(String, int)
by combining the class name and the shortName as the ID.HMACConfidentialKey(String id)
Calls intoHMACConfidentialKey(String, int)
with the longest possible HMAC length.HMACConfidentialKey(String id, int length)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
checkMac(byte[] message, byte[] mac)
Convenience method for verifying the MAC code.boolean
checkMac(String message, String mac)
Verifies MAC constructed frommac(String)
Mac
createMac()
Creates a newMac
object.byte[]
mac(byte[] message)
Computes the message authentication code for the specified byte sequence.String
mac(String message)
Computes the message authentication code and return it as a string.-
Methods inherited from class jenkins.security.ConfidentialKey
getId, load, store
-
-
-
-
Constructor Detail
-
HMACConfidentialKey
public HMACConfidentialKey(String id, int length)
- Parameters:
length
- Byte length of the HMAC code. By default we use HMAC-SHA256, which produces 256bit (=32bytes) HMAC, but if different use cases requires a shorter HMAC, specify the desired length here. Note that when usingmac(String)
, string encoding causes the length to double. So if you want to get 16-letter HMAC, you specify 8 here.
-
HMACConfidentialKey
public HMACConfidentialKey(String id)
Calls intoHMACConfidentialKey(String, int)
with the longest possible HMAC length.
-
HMACConfidentialKey
public HMACConfidentialKey(Class owner, String shortName, int length)
Calls intoHMACConfidentialKey(String, int)
by combining the class name and the shortName as the ID.
-
-
Method Detail
-
mac
public byte[] mac(byte[] message)
Computes the message authentication code for the specified byte sequence.
-
checkMac
public boolean checkMac(byte[] message, byte[] mac)
Convenience method for verifying the MAC code.
-
mac
public String mac(String message)
Computes the message authentication code and return it as a string. While redundant, often convenient.
-
checkMac
public boolean checkMac(String message, String mac)
Verifies MAC constructed frommac(String)
-
-