Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: hmac sha256

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default hmac sha256

    Hi, I have to derive the signing key (encyption mode hmac sha256) for an aws project. This is my problem, but something is going wrong I think because I'm not able to call the two methods at the end of the program in the right way. And also I'm not able to print the final value of ksigning. Can you help me?
    I report my code. Thank you very much.



    package signature;
     
    import org.apache.commons.codec.binary.Hex;
    //import java.io.UnsupportedEncodingException;
    //import java.security.InvalidKeyException;
    //import java.security.NoSuchAlgorithmException;
     
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidKeyException;
     
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
     
     
    public class signing_key3 {
    	public static void main(String[] args)  
    	{
    		String key="8cTZJ5chnb7YcUszssveBXZDTvvhhUFQd3YACxFp";
    		String dateStamp="20180713T111300Z";
    		String regionName="eu-west-1";
    		String serviceName="iotdata";
    		String data="20180713";
    		String algorithm="HmacSHA256";
    		//byte[] key1=key.getBytes();
    		//byte[] data1=data.getBytes();
    		try
    		{
    		Mac HmacSHA256 =Mac.getInstance(algorithm);
    		 SecretKeySpec kSecret = new SecretKeySpec(key.getBytes("UTF-8"), algorithm); 
    		// HmacSHA256.init(kSecret);
    		 //String foo = "I am a string";
             //byte[] kSecret1=kSecret.getBytes();
             //System.out.println( Hex.encodeHexString( kSecret1));
    		 /* byte[] key1=key.getBytes();*/
    			/*HmacSHA256.HmacSHA256(/*String data, String key);*/
    			/*HmacSHA256.getSignatureKey(/*String key, String dateStamp, String regionName, String serviceName);*/
     
    		//byte[] s = HmacSHA256( data,  key);
    		//HmacSHA256( data,  key);
     
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		//System.out.println(kSigning);
    	}
     
    	//class HmacSHA256{
    		static byte[] HmacSHA256( String data, String key) throws Exception 
     
    	{
    		key="8cTZJ5chnb7YcUszssveBXZDTvvhhUFQd3YACxFp";
    		data="20180713";
    		String algorithm="HmacSHA256";
     
    		byte[] key1=key.getBytes();
    	   // byte[] data1=data.getBytes();
    		Mac mac = Mac.getInstance(algorithm);
    	    mac.init(new SecretKeySpec(key1, algorithm));
    	    return mac.doFinal(data.getBytes("UTF8"));
    	}
     
    	//class getSignatureKey{
    	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);
    		//String kSigning1=kSigning.toString();
    		System.out.println( Hex.encodeHexString( kSigning));
    		return kSigning;
    	}
    }
    Last edited by Norm; July 16th, 2018 at 05:43 AM. Reason: Added code tags

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hmac sha256

    I'm not able to call the two methods at the end of the program in the right way. And also I'm not able to print the final value of ksigning
    Please show what happens when you compile and execute the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hmac sha256

    I'm using eclipse: there is no error but nothing shown on console.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hmac sha256

    Does the code call any methods to execute a print statement that will show on the console?

    The code that could call a method is commented out. Remove the //s and the /* */s so that the code will compile and execute.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hmac sha256

    I did some little changes and removed comments and tryed to print kSigning. Follows the code and error message. It seems that I'm not able to call the two methods inside the main.

    Java code

    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)
    {
    String key="8cTZJ5chnb7YcUszssveBXZDTvvhhUFQd3YACxFp";
    String dateStamp="20180713T111300Z";
    String regionName="eu-west-1";
    String serviceName="iotdata";
    String data="20180713";
    String algorithm="HmacSHA256";

    try
    {
    Mac HmacSHA256 =Mac.getInstance(algorithm);
    SecretKeySpec kSecret = new SecretKeySpec(key.getBytes("UTF-8"), algorithm);
    System.out.println(kSigning);

    }
    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;
    }

    }

    /code


    MESSAGE

    kSigning cannot be resolved to a variable

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hmac sha256

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    kSigning cannot be resolved to a variable
    Where does that error happen?
    The compiler can not find a definition for kSigning that is in scope for where it is used.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hmac sha256

    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;
    	}
     
    	}

Similar Threads

  1. Implementing Sha256 and not getting the result I should get
    By ydan87 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 26th, 2011, 03:30 PM