Created
February 16, 2017 09:39
-
-
Save tcataldo/2bfc91f2d150fd0bee7d95a76d3d492a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* BEGIN LICENSE | |
* Copyright © Blue Mind SAS, 2012-2017 | |
* | |
* This file is part of BlueMind. BlueMind is a messaging and collaborative | |
* solution. | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of either the GNU Affero General Public License as | |
* published by the Free Software Foundation (version 3 of the License). | |
* | |
* This program 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 LICENSE.txt | |
* END LICENSE | |
*/ | |
package net.bluemind.signature; | |
import static org.junit.Assert.assertTrue; | |
import java.security.InvalidKeyException; | |
import java.security.KeyPair; | |
import java.security.KeyPairGenerator; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.NoSuchProviderException; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.security.SecureRandom; | |
import java.security.Signature; | |
import java.security.SignatureException; | |
import java.util.Random; | |
import org.junit.Test; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.buffer.ByteBufUtil; | |
import io.netty.buffer.Unpooled; | |
public class KeysHelper { | |
private PrivateKey priv; | |
private PublicKey pub; | |
public void setup() throws NoSuchAlgorithmException, NoSuchProviderException { | |
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN"); | |
SecureRandom random = SecureRandom.getInstanceStrong(); | |
keyGen.initialize(1024, random); | |
KeyPair pair = keyGen.generateKeyPair(); | |
this.priv = pair.getPrivate(); | |
this.pub = pair.getPublic(); | |
} | |
public byte[] sign(byte[] data) | |
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException { | |
Signature dsa = signature(); | |
dsa.initSign(priv); | |
dsa.update(data); | |
byte[] signed = dsa.sign(); | |
return signed; | |
} | |
private Signature signature() throws NoSuchAlgorithmException, NoSuchProviderException { | |
return Signature.getInstance("SHA1withDSA", "SUN"); | |
} | |
public boolean verify(byte[] data, byte[] signed) | |
throws SignatureException, InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException { | |
Signature sig = signature(); | |
sig.initVerify(pub); | |
sig.update(data); | |
return sig.verify(signed); | |
} | |
@Test | |
public void testKeyHelper() throws Exception { | |
Random rd = new Random(); | |
ByteBuf token = Unpooled.buffer(); | |
token.writeLong(rd.nextLong()).writeLong(rd.nextLong()); | |
String tokenHex = ByteBufUtil.hexDump(token); | |
System.out.println("To sign: " + tokenHex); | |
byte[] data = new byte[16]; | |
token.readBytes(data); | |
KeysHelper kh = this; | |
kh.setup(); | |
byte[] signature = kh.sign(data); | |
ByteBuf sig = Unpooled.wrappedBuffer(signature); | |
String sigHex = ByteBufUtil.hexDump(sig); | |
System.out.println("Sig: " + sigHex); | |
System.out.println("For the login page: " + tokenHex + ":" + sigHex); | |
boolean verified = kh.verify(data, signature); | |
assertTrue(verified); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment