This commit is contained in:
Looly
2020-09-17 11:26:14 +08:00
parent 6b89c841d3
commit 85601e2446
5 changed files with 32 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
package cn.hutool.captcha.generator;
import cn.hutool.core.math.Calculator;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
@@ -59,20 +60,8 @@ public class MathGenerator implements CodeGenerator {
return false;
}
final int a = Integer.parseInt(StrUtil.sub(code, 0, this.numberLength).trim());
final char operator = code.charAt(this.numberLength);
final int b = Integer.parseInt(StrUtil.sub(code, this.numberLength + 1, this.numberLength + 1 + this.numberLength).trim());
switch (operator) {
case '+':
return (a + b) == result;
case '-':
return (a - b) == result;
case '*':
return (a * b) == result;
default:
return false;
}
final int calculateResult = (int) Calculator.conversion(code);
return result == calculateResult;
}
/**

View File

@@ -0,0 +1,14 @@
package cn.hutool.captcha;
import cn.hutool.captcha.generator.MathGenerator;
import org.junit.Test;
public class GeneratorTest {
@Test
public void mathGeneratorTest(){
final MathGenerator mathGenerator = new MathGenerator();
for (int i = 0; i < 1000; i++) {
mathGenerator.verify(mathGenerator.generate(), "0");
}
}
}