Class Key


  • public final class Key
    extends java.lang.Object
    Represents a WireGuard public or private key. This class uses specialized constant-time base64 and hexadecimal codec implementations that resist side-channel attacks.

    Instances of this class are immutable.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Key.Format
      The supported formats for encoding a WireGuard key.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      static Key fromBase64​(java.lang.String str)
      Decodes a WireGuard public or private key from its base64 string representation.
      static Key fromBytes​(byte[] bytes)
      Wraps a WireGuard public or private key in an immutable container.
      static Key fromHex​(java.lang.String str)
      Decodes a WireGuard public or private key from its hexadecimal string representation.
      byte[] getBytes()
      Returns the key as an array of bytes.
      int hashCode()  
      java.lang.String toBase64()
      Encodes the key to base64.
      java.lang.String toHex()
      Encodes the key to hexadecimal ASCII characters.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • fromBase64

        public static Key fromBase64​(java.lang.String str)
                              throws KeyFormatException
        Decodes a WireGuard public or private key from its base64 string representation. This function throws a KeyFormatException if the source string is not well-formed.
        Parameters:
        str - the base64 string representation of a WireGuard key
        Returns:
        the decoded key encapsulated in an immutable container
        Throws:
        KeyFormatException
      • fromBytes

        public static Key fromBytes​(byte[] bytes)
                             throws KeyFormatException
        Wraps a WireGuard public or private key in an immutable container. This function throws a KeyFormatException if the source data is not the correct length.
        Parameters:
        bytes - an array of bytes containing a WireGuard key in binary format
        Returns:
        the key encapsulated in an immutable container
        Throws:
        KeyFormatException
      • fromHex

        public static Key fromHex​(java.lang.String str)
                           throws KeyFormatException
        Decodes a WireGuard public or private key from its hexadecimal string representation. This function throws a KeyFormatException if the source string is not well-formed.
        Parameters:
        str - the hexadecimal string representation of a WireGuard key
        Returns:
        the decoded key encapsulated in an immutable container
        Throws:
        KeyFormatException
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • getBytes

        public byte[] getBytes()
        Returns the key as an array of bytes.
        Returns:
        an array of bytes containing the raw binary key
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toBase64

        public java.lang.String toBase64()
        Encodes the key to base64.
        Returns:
        a string containing the encoded key
      • toHex

        public java.lang.String toHex()
        Encodes the key to hexadecimal ASCII characters.
        Returns:
        a string containing the encoded key