File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\jenkins\security\HexStringConfidentialKey.java.html

1 package jenkins.security;
2
3 import hudson.Util;
4
5 import java.io.IOException;
6
7 /**
8 * {@link ConfidentialKey} that is the random hexadecimal string of length N.
9 *
10 * <p>
11 * This is typically used as a unique ID, as a hex dump is suitable for printing, copy-pasting,
12 * as well as use as an identifier.
13 *
14 * @author Kohsuke Kawaguchi
15 * @since 1.498
16 */
17 public class HexStringConfidentialKey extends ConfidentialKey {
18 private final int length;
19
20 private volatile String secret;
21
22 /**
23 * @param length
24 * Length of the hexadecimal string.
25 */
26 public HexStringConfidentialKey(String id, int length) {
27 super(id);
28 if (length%2!=0)
29 throw new IllegalArgumentException("length must be even: "+length);
30 this.length = length;
31 }
32
33 public HexStringConfidentialKey(Class owner, String shortName, int length) {
34 this(owner.getName()+'.'+shortName,length);
35 }
36
37 /**
38 * Returns the persisted hex string value.
39 *
40 * If the value isn't persisted, a new random value is created.
41 *
42 * @throws Error
43 * If the secret fails to load. Not throwing a checked exception is for the convenience
44 * of the caller.
45 */
46 public String get() {
47 try {
48 if (secret==null) {
49 synchronized (this) {
50 if (secret==null) {
51 byte[] payload = load();
52 if (payload==null) {
53 payload = ConfidentialStore.get().randomBytes(length/2);
54 store(payload);
55 }
56 secret = Util.toHexString(payload).substring(0,length);
57 }
58 }
59 }
60 return secret;
61 } catch (IOException e) {
62 throw new Error("Failed to load the key: "+getId(),e);
63 }
64 }
65 }
66