Top Description Fields Constructors Methods
org.eclipse.jgit.transport

public final Class AwsRequestSignerV4

extends Object
Class Inheritance
Static Imports
java.nio.charset.StandardCharsets.UTF_8

Utility class for signing requests to AWS service endpoints using the V4 signing protocol. Reference implementation: AWSS3SigV4JavaSamples.zip
Since
5.13.1
See Also
AWS Signature Version 4

Field Summary

Modifier and TypeField and Description
private static final String
ALGORITHM

AWS version 4 signing algorithm (for authorization header).

private static final DateTimeFormatter
AMZ_DATE_FORMAT

Date format for the 'x-amz-date' header.

private static final String
EMPTY_BODY_SHA256

SHA-256 hash of an empty request body.

private static final String
MAC_ALGORITHM

Java Message Authentication Code (MAC) algorithm name.

private static final String
SCHEME

AWS version 4 signing scheme.

private static final DateTimeFormatter
SCOPE_DATE_FORMAT

Date format for the string-to-sign's scope.

private static final String
TERMINATOR

AWS version 4 terminator string.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

Hex-encoded SHA-256 hash of the provided byte array.
calculateBodyHash
(final byte[]
Byte array to hash
data
)

Calculates the hex-encoded SHA-256 hash of the provided byte array.

private static String

Returns:

The canonical headers with values for the request.
getCanonicalizedHeaderString
(Map<String, String>
Map containing all request headers.
headers
)

Constructs the canonical header string for a request.

public static String

Returns:

The canonicalized query string for the request.
getCanonicalizedQueryString
(Map<String, String>
The query parameters in the request.
queryParameters
)

Constructs the canonicalized query string for a request.

private static String

Returns:

The canonicalized resource path for the AWS service endpoint.
getCanonicalizedResourcePath
(URL
The AWS service endpoint URL, including the path to any resource.
url
)

Constructs the canonicalized resource path for an AWS service endpoint.

private static String

Returns:

String that lists all request headers in sorted case-insensitive order, separated by a ';'.
getCanonicalizeHeaderNames
(Map<String, String>
Map containing all request headers.
headers
)

Construct a string listing all request headers in sorted case-insensitive order, separated by a ';'.

public static byte[]

Returns:

Hashed string contents of the provided byte array.
hash
(byte[]
The byte array to hash.
data
)

Hashes the provided byte array using the SHA-256 algorithm.

public static void
sign(HttpURLConnection
The request to sign.
httpURLConnection
,
Map<String, String>
The query parameters being sent in the request.
queryParameters
,
long
The content length of the data being sent in the request
contentLength
,
String
Hex-encoded SHA-256 hash of the data being sent in the request
bodyHash
,
String
The signing name of the AWS service (e.g. "s3").
serviceName
,
String
The name of the AWS region that will handle the request (e.g. "us-east-1").
regionName
,
String
The user's AWS Access Key.
awsAccessKey
,
char[]
The user's AWS Secret Key.
awsSecretKey
)

Sign the provided request with an AWS4 signature as the 'Authorization' header.

private static byte[]

Returns:

Signed string data.
signStringWithKey
(String
The string data to sign.
stringToSign
,
byte[]
The key material of the secret key.
key
)

Signs the provided string data using the specified key.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ALGORITHMback to summary
private static final String ALGORITHM

AWS version 4 signing algorithm (for authorization header).

AMZ_DATE_FORMATback to summary
private static final DateTimeFormatter AMZ_DATE_FORMAT

Date format for the 'x-amz-date' header.

EMPTY_BODY_SHA256back to summary
private static final String EMPTY_BODY_SHA256

SHA-256 hash of an empty request body.

MAC_ALGORITHMback to summary
private static final String MAC_ALGORITHM

Java Message Authentication Code (MAC) algorithm name.

SCHEMEback to summary
private static final String SCHEME

AWS version 4 signing scheme.

SCOPE_DATE_FORMATback to summary
private static final DateTimeFormatter SCOPE_DATE_FORMAT

Date format for the string-to-sign's scope.

TERMINATORback to summary
private static final String TERMINATOR

AWS version 4 terminator string.

Constructor Detail

AwsRequestSignerV4back to summary
private AwsRequestSignerV4()

Method Detail

calculateBodyHashback to summary
public static String calculateBodyHash(final byte[] data)

Calculates the hex-encoded SHA-256 hash of the provided byte array.

Parameters
data:byte[]

Byte array to hash

Returns:String

Hex-encoded SHA-256 hash of the provided byte array.

getCanonicalizedHeaderStringback to summary
private static String getCanonicalizedHeaderString(Map<String, String> headers)

Constructs the canonical header string for a request.

Parameters
headers:Map<String, String>

Map containing all request headers.

Returns:String

The canonical headers with values for the request.

getCanonicalizedQueryStringback to summary
public static String getCanonicalizedQueryString(Map<String, String> queryParameters)

Constructs the canonicalized query string for a request.

Parameters
queryParameters:Map<String, String>

The query parameters in the request.

Returns:String

The canonicalized query string for the request.

getCanonicalizedResourcePathback to summary
private static String getCanonicalizedResourcePath(URL url)

Constructs the canonicalized resource path for an AWS service endpoint.

Parameters
url:URL

The AWS service endpoint URL, including the path to any resource.

Returns:String

The canonicalized resource path for the AWS service endpoint.

getCanonicalizeHeaderNamesback to summary
private static String getCanonicalizeHeaderNames(Map<String, String> headers)

Construct a string listing all request headers in sorted case-insensitive order, separated by a ';'.

Parameters
headers:Map<String, String>

Map containing all request headers.

Returns:String

String that lists all request headers in sorted case-insensitive order, separated by a ';'.

hashback to summary
public static byte[] hash(byte[] data)

Hashes the provided byte array using the SHA-256 algorithm.

Parameters
data:byte[]

The byte array to hash.

Returns:byte[]

Hashed string contents of the provided byte array.

signback to summary
public static void sign(HttpURLConnection httpURLConnection, Map<String, String> queryParameters, long contentLength, String bodyHash, String serviceName, String regionName, String awsAccessKey, char[] awsSecretKey)

Sign the provided request with an AWS4 signature as the 'Authorization' header.

Parameters
httpURLConnection:HttpURLConnection

The request to sign.

queryParameters:Map<String, String>

The query parameters being sent in the request.

contentLength:long

The content length of the data being sent in the request

bodyHash:String

Hex-encoded SHA-256 hash of the data being sent in the request

serviceName:String

The signing name of the AWS service (e.g. "s3").

regionName:String

The name of the AWS region that will handle the request (e.g. "us-east-1").

awsAccessKey:String

The user's AWS Access Key.

awsSecretKey:char[]

The user's AWS Secret Key.

signStringWithKeyback to summary
private static byte[] signStringWithKey(String stringToSign, byte[] key)

Signs the provided string data using the specified key.

Parameters
stringToSign:String

The string data to sign.

key:byte[]

The key material of the secret key.

Returns:byte[]

Signed string data.