Package com.wireguard.crypto
Class Key
- java.lang.Object
-
- com.wireguard.crypto.Key
-
public final class Key extends java.lang.ObjectRepresents 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 classKey.FormatThe supported formats for encoding a WireGuard key.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)static KeyfromBase64(java.lang.String str)Decodes a WireGuard public or private key from its base64 string representation.static KeyfromBytes(byte[] bytes)Wraps a WireGuard public or private key in an immutable container.static KeyfromHex(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.inthashCode()java.lang.StringtoBase64()Encodes the key to base64.java.lang.StringtoHex()Encodes the key to hexadecimal ASCII characters.
-
-
-
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 aKeyFormatExceptionif 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 aKeyFormatExceptionif 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 aKeyFormatExceptionif 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:
equalsin classjava.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:
hashCodein classjava.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
-
-