刘光辉
119 分钟以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package jnpf.elnService.impl;
 
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
final class ElnRepeatAuditEventIds {
    private static final int MAX_CLIENT_EVENT_ID_LENGTH = 70;
 
    private ElnRepeatAuditEventIds() {
    }
 
    static String of(String operationCode, String phase) {
        String candidate = operationCode + ":" + phase;
        if (candidate.length() <= MAX_CLIENT_EVENT_ID_LENGTH) return candidate;
        return "RPT-" + sha256(candidate);
    }
 
    private static String sha256(String source) {
        try {
            byte[] digest = MessageDigest.getInstance("SHA-256")
                    .digest(source.getBytes(StandardCharsets.UTF_8));
            StringBuilder result = new StringBuilder(64);
            for (byte value : digest) result.append(String.format("%02x", value & 0xff));
            return result.toString();
        } catch (NoSuchAlgorithmException exception) {
            throw new IllegalStateException("SHA-256 不可用", exception);
        }
    }
}