cn.z.qrcode.encoder.GenericGF Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qrcode-encoder Show documentation
Show all versions of qrcode-encoder Show documentation
QR Code Encoder For Java 二维码生成器Java版
The newest version!
package cn.z.qrcode.encoder;
/**
* 通用Galois Fields域(通用伽罗华域)
*
* 仅适用于QrCode
*
*
* createDate 2023/05/29 11:11:11
*
*
* @author ALI[[email protected]]
* @since 1.0.0
**/
public class GenericGF {
/**
* 维度
* 256
*/
private static final int DIMENSION = 256;
/**
* 多项式
* 0x011D -> 0000 0001 0001 1101 -> x^8 + x^4 + x^3 + x^2 + 1
*/
private static final int POLY = 0x011D;
/**
* 指数表
*/
private static final int[] ExpTable;
/**
* 对数表
*/
private static final int[] LogTable;
static {
// 初始化指数表和对数表
ExpTable = new int[DIMENSION];
LogTable = new int[DIMENSION];
int x = 1;
for (int i = 0; i < DIMENSION; i++) {
ExpTable[i] = x;
x <<= 1;
if (x >= DIMENSION) {
x ^= POLY;
x &= DIMENSION - 1;
}
}
for (int i = 0; i < DIMENSION - 1; i++) {
LogTable[ExpTable[i]] = i;
}
}
private GenericGF() {
}
/**
* 加法
*/
public static int Addition(int a, int b) {
return a ^ b;
}
/**
* 2的次方
*/
public static int Exp(int a) {
return ExpTable[a];
}
/**
* 逆运算
*/
public static int Inverse(int a) {
return ExpTable[DIMENSION - LogTable[a] - 1];
}
/**
* 乘法
*/
public static int Multiply(int a, int b) {
if (a == 0 || b == 0) {
return 0;
}
return ExpTable[(LogTable[a] + LogTable[b]) % (DIMENSION - 1)];
}
}