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

41 lines
1.3 KiB
Java

package io.github.lonamiwebs.overgram.crypto;
import io.github.lonamiwebs.overgram.utils.Utils;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
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) {
// Big integer is big endian but we need little endian
final byte[] data = Utils.sha1digest(Utils.concat(
Utils.reversed(newNonce.toByteArray()), Utils.concat(
ByteBuffer.allocate(1).put((byte) number).array(),
ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(auxHash).array()
)
));
// Little endian once again so read (20..4]
final byte[] numberBytes = new byte[16];
for (int i = 16; i-- != 0; ) {
numberBytes[i] = data[19 - i];
}
return new BigInteger(numberBytes);
}
}