Azure / Azure/azure-rest-api-specs
Unable to generate Signature for Azure Communication in java
- Dominant language
- TypeSpec
- Stars
- 3.1k
- Forks
- 5.9k
- Avg merge
- 2d 22h
- Merged PRs (30d)
- 444
Description
Can somebody help me ? Trying to create the Authorization Signature and Hash, with Access Key using Google Guava Library in Java, but consistently getting : 'x-ms-content-sha256' differs from generated content hash : with Http Status 401
Objective : Trying to send an email via Rest API
Content in reqBody is an Object.
Code Below:
```
String contentStr = new ObjectMapper().writeValueAsString(reqBody.getContent());
contentStr= new String(contentStr.getBytes(), StandardCharsets.UTF_8);
String contentHash256 = Base64.getEncoder().encodeToString(Hashing.sha256().hashString(contentStr, StandardCharsets.UTF_8).asBytes());
String stringToSign = reqBody.getMethod() + "\n" + reqBody.getPathAndQuery() + "\n" + reqBody.getTimestamp() + ";" + reqBody.getHost() + ";" + contentHash256;
stringToSign=new String(stringToSign.getBytes(), StandardCharsets.UTF_8).trim();
String signature = Base64.getEncoder().encodeToString(Hashing.hmacSha256(Base64.getDecoder().decode(reqBody.getAccessKey()))
.hashString(data, StandardCharsets.UTF_8).asBytes());
AzureCommunicationAuthHeaderResponse response = new AzureCommunicationAuthHeaderResponse();
response.setContentSha256(contentHash256);
response.setDate(reqBody.getTimestamp());
response.setHost(reqBody.getPathAndQuery());
response.setAuthorization("HMAC-SHA256 SignedHeaders=x-ms-date;host;x-ms-content-sha256&Signature=" + signature);
```
Contributor guide
Research direction
No repository file or test is named. Start by tracing how contentStr, pathAndQuery, host, timestamp, and the HMAC input are assembled in the shown Java code, then compare those values with the Azure Communication Services email REST API signing requirements. Done means identifying the source of the content-hash mismatch and documenting a request that no longer returns HTTP 401.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, java
- Domain
- api, authentication, cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100