Skip to content

Instantly share code, notes, and snippets.

@Romain-P
Created October 23, 2017 22:14
Show Gist options
  • Select an option

  • Save Romain-P/e1cd63e285bf81ccac4803a6ef90ca73 to your computer and use it in GitHub Desktop.

Select an option

Save Romain-P/e1cd63e285bf81ccac4803a6ef90ca73 to your computer and use it in GitHub Desktop.
package org.heat.dofus.network;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.fungsi.Try;
import org.heat.dofus.network.netty.NettyDataReader;
import org.heat.dofus.network.netty.NettyDataWriter;
import org.heat.shared.Md5;
import org.heat.shared.RsaHandler;
import java.security.SecureRandom;
/**
* Author: romain
* Date: 23/10/2017
*/
public class DataSigner {
private final SecureRandom generator;
private final RsaHandler rsa;
private static final String ANKAMA_HEADER = "ASKF";
private static final String SIGN_HEADER = "AKSD";
public DataSigner() {
this.rsa = Try
.of(() -> new RsaHandler("public", "private", RsaHandler.RSA_PKCS1_PADDING))
.getOrThrow();
this.generator = new SecureRandom();
}
public ByteBuf sign(ByteBuf data, boolean includeData) throws Exception {
NettyDataReader reader = NettyDataReader.of(data);
NettyDataWriter writer = NettyDataWriter.of(Unpooled.buffer());
byte random = (byte) generator.nextInt(8);
NettyDataWriter hash = NettyDataWriter.of(Unpooled.buffer());
hash.writeInt8(random);
hash.writeUInt16(data.readableBytes());
hash.writeUTFBytes(Md5.hash(reader.readUTF(data.readableBytes()).getBytes()));
byte[] array = hash.getUnderlyingBuf().array();
for (int i = 2; i < array.length; i++)
array[i] = (byte) (array[i] ^ random);
hash.setPosition(0);
byte[] ciphered = rsa.encrypt(hash.getUnderlyingBuf().array());
writer.writeUTFBytes(ANKAMA_HEADER);
writer.writeInt16((short) 1);
writer.writeInt32(ciphered.length);
writer.writeBytes(ciphered);
if (includeData)
writer.writeBytes(data.array());
return writer.getUnderlyingBuf();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment