Finally I solved! Ipost the orrect code, perhaps it can help someone. There were two problems: the first was that I was not calling the methods inside the main, the second was that I should rename the variable kSigning inside the main to print it bacause the method rename just the value, not the variable. Thank you very much for your help!
package signature;
import org.apache.commons.codec.binary.Hex;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class signing_key3a {
public static void main(String[] args) throws Exception
{
String key="8cTZJ5chnb7YcUszssveBXZDTvvhhUFQd3YACxFp";
String dateStamp="20180713T111300Z";
String regionName="eu-west-1";
String serviceName="iotdata";
String data="20180713";
String algorithm="HmacSHA256";
//byte[] kSigning2 = getSignatureKey( key, dateStamp, regionName, serviceName);
HmacSHA256( data, key);
try
{
//getSignatureKey( key, dateStamp, regionName, serviceName);
Mac HmacSHA256 =Mac.getInstance(algorithm);
SecretKeySpec kSecret = new SecretKeySpec(key.getBytes("UTF-8"), algorithm);
//System.out.println(kSigning2);
byte[] kSigning2 = getSignatureKey( key, dateStamp, regionName, serviceName);
}
catch(Exception e)
{
e.printStackTrace();
}
}
static byte[] HmacSHA256( String data, String key) throws Exception
{
key="8cTZJ5chnb7YcUszssveBXZDTvvhhUFQd3YACxFp";
data="20180713";
String algorithm="HmacSHA256";
byte[] key1=key.getBytes();
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key1, algorithm));
return mac.doFinal(data.getBytes("UTF8"));
}
static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception
{
key="8cTZJ5chnb7YcUszssveBXZDTvvhhUFQd3YACxFp";
dateStamp="20180713T111300Z";
regionName="eu-west-1";
serviceName="iotdata";
String algorithm="HmacSHA256";
byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
String kSecret1=kSecret.toString();
byte[] kDate =HmacSHA256(dateStamp, kSecret1);
String kDate1=kDate.toString();
byte[] kRegion = HmacSHA256(regionName, kDate1);
String kRegion1=kRegion.toString();
byte[] kService = HmacSHA256(serviceName, kRegion1);
String kService1=kService.toString();
byte[] kSigning = HmacSHA256("aws4_request", kService1);
System.out.println( Hex.encodeHexString( kSigning));
return kSigning;
}
}