io.continual.util.data.Sha1HmacSigner Maven / Gradle / Ivy
/*
* Copyright 2019, Continual.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.continual.util.data;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Sha1HmacSigner
{
private Sha1HmacSigner() {
}
private static final String kHmacSha1Algo = "HmacSHA1";
public static String sign ( String message, String key )
{
try
{
final SecretKey secretKey = new SecretKeySpec ( key.getBytes (), kHmacSha1Algo );
final Mac mac = Mac.getInstance ( kHmacSha1Algo );
mac.init ( secretKey );
final byte[] rawHmac = mac.doFinal ( message.getBytes () );
return TypeConvertor.base64Encode ( rawHmac );
}
catch ( InvalidKeyException e )
{
throw new RuntimeException ( e );
}
catch ( NoSuchAlgorithmException e )
{
throw new RuntimeException ( e );
}
catch ( IllegalStateException e )
{
throw new RuntimeException ( e );
}
}
// static public void main ( String args[] )
// {
// if ( args.length != 2 )
// {
// System.err.println ( "usage: Sha1HmacSigner " );
// }
// else if ( args.length == 2 )
// {
// System.out.println ( sign ( args[0], args[1] ) );
// }
// }
}