1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.wss4j.common.derivedKey;
20
21 import java.nio.charset.StandardCharsets;
22
23 import org.apache.wss4j.common.ext.WSSecurityException;
24
25 public final class DerivedKeyUtils {
26
27 private DerivedKeyUtils() {
28
29 }
30
31
32
33
34
35
36
37
38
39 public static byte[] deriveKey(String algorithm, String label, int length, byte[] secret, byte[] nonce, int offset)
40 throws WSSecurityException {
41 DerivationAlgorithm algo = AlgoFactory.getInstance(algorithm);
42 byte[] labelBytes;
43 if (label == null || label.length() == 0) {
44 String defaultLabel = ConversationConstants.DEFAULT_LABEL + ConversationConstants.DEFAULT_LABEL;
45 labelBytes = defaultLabel.getBytes(StandardCharsets.UTF_8);
46 } else {
47 labelBytes = label.getBytes(StandardCharsets.UTF_8);
48 }
49
50 byte[] seed = new byte[labelBytes.length + nonce.length];
51 System.arraycopy(labelBytes, 0, seed, 0, labelBytes.length);
52 System.arraycopy(nonce, 0, seed, labelBytes.length, nonce.length);
53
54 long keyLength = length;
55 if (keyLength <= 0) {
56 keyLength = 32L;
57 }
58 return algo.createKey(secret, seed, offset, keyLength);
59 }
60 }