Example usage for org.springframework.vault.authentication Sha256 toHexString

List of usage examples for org.springframework.vault.authentication Sha256 toHexString

Introduction

In this page you can find the example usage for org.springframework.vault.authentication Sha256 toHexString.

Prototype

static String toHexString(byte[] bytes) 

Source Link

Usage

From source file:org.springframework.vault.authentication.MacAddressUserId.java

@Override
public String createUserId() {

    try {/*from w  w w . j a va2 s. co  m*/

        NetworkInterface networkInterface = null;
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

        if (StringUtils.hasText(networkInterfaceHint)) {

            try {
                networkInterface = getNetworkInterface(Integer.parseInt(networkInterfaceHint), interfaces);
            } catch (NumberFormatException e) {
                networkInterface = getNetworkInterface((networkInterfaceHint), interfaces);
            }
        }

        if (networkInterface == null) {

            if (StringUtils.hasText(networkInterfaceHint)) {
                log.warn(String.format("Did not find a NetworkInterface applying hint %s",
                        networkInterfaceHint));
            }

            InetAddress localHost = InetAddress.getLocalHost();
            networkInterface = NetworkInterface.getByInetAddress(localHost);

            if (networkInterface == null) {
                throw new IllegalStateException(
                        String.format("Cannot determine NetworkInterface for %s", localHost));
            }
        }

        byte[] mac = networkInterface.getHardwareAddress();
        if (mac == null) {
            throw new IllegalStateException(
                    String.format("Network interface %s has no hardware address", networkInterface.getName()));
        }

        return Sha256.toSha256(Sha256.toHexString(mac));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}