Overgram/lib/src/main/java/io/github/lonamiwebs/overgram/crypto/AuthKey.java

35 lines
1.0 KiB
Java

package io.github.lonamiwebs.overgram.crypto;
import io.github.lonamiwebs.overgram.utils.BinaryWriter;
import io.github.lonamiwebs.overgram.utils.Utils;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
public class AuthKey {
public final byte[] key;
public final long auxHash;
public final long keyId;
public AuthKey(final byte[] key) {
this.key = key;
final ByteBuffer digest = ByteBuffer.wrap(Utils.sha1digest(key)).order(ByteOrder.LITTLE_ENDIAN);
auxHash = digest.getLong();
digest.position(digest.position() + 4);
keyId = digest.getLong();
}
public BigInteger calcNewNonceHash(final BigInteger newNonce, final int number) {
final BinaryWriter writer = new BinaryWriter(41);
writer.write(newNonce, 32);
writer.write((byte) number);
writer.write(auxHash);
final byte[] sha = Utils.sha1digest(writer.toBytes());
return new BigInteger(Utils.reversed(Arrays.copyOfRange(sha, 4, 20)));
}
}