Created
October 23, 2017 23:03
-
-
Save Romain-P/aa928ad093b997d4d19bcc0a9d8dafda to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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(String privateKey) { | |
| this.rsa = Try | |
| .of(() -> new RsaHandler(null, privateKey, 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(), true); | |
| 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