Java tutorial
/** * Copyright 2014 Andy Godwin * * 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 spring.travel.site.auth; import org.springframework.beans.factory.annotation.Value; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class Signer { private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; private static char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; @Value("${application.secret}") private String key; public String sign(String data) throws AuthException { try { SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] raw = mac.doFinal(data.getBytes(StandardCharsets.UTF_8)); return toHex(raw); } catch (InvalidKeyException | NoSuchAlgorithmException e) { throw new AuthException("Failed signing data", e); } } private String toHex(byte[] data) { char[] chars = new char[data.length * 2]; for (int i = 0; i < data.length; i++) { int b = data[i] & 0xff; chars[2 * i] = hexChars[b >> 4]; chars[2 * i + 1] = hexChars[b & 0xf]; } return new String(chars); } }