Java tutorial
//package com.java2s; /* * Advance Sudoku - a sudoku puzzle game for Android. * Copyright 2015 Joachim Thilo * * This file is part of Andoku. * * Advance Sudoku is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Advance Sudoku is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Advance Sudoku . If not, see <http://www.gnu.org/licenses/>. */ import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class Main { private static final String MAC_NAME = "HmacSHA1"; private static byte[] sign(String keyString, byte[] message) throws GeneralSecurityException { byte[] keyBytes = utf8(keyString); SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME); Mac mac = Mac.getInstance(MAC_NAME); mac.init(key); return mac.doFinal(message); } private static byte[] utf8(String keyString) { try { return keyString.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); // utf-8 is supported! } } }