use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.
the class CaptchaApiImpl method generateCaptcha.
@Override
public GenerateCaptchaResult generateCaptcha(GenerateCaptchaRequest generateCaptchaRequest) {
Preconditions.checkNotNull(generateCaptchaRequest);
Captcha captcha = captchaService.generate();
GenerateCaptchaResult result = new GenerateCaptchaResult();
result.token = captcha.getToken();
result.pngImageDataBase64 = BaseEncoding.base64().encode(captcha.getPngImageData());
return result;
}
use of org.haiku.haikudepotserver.captcha.model.Captcha in project haikudepotserver by haiku.
the class SimpleMathProblemCaptchaAlgorithm method generate.
@Override
public synchronized Captcha generate() {
int addend1 = Math.abs(random.nextInt() % 25);
int addend2 = Math.abs(random.nextInt() % 25);
String problem = String.format("%d + %d", addend1, addend2);
String response = Integer.toString(addend1 + addend2);
byte[] pngImageData;
synchronized (this) {
// reset the image.
bufferedImageGraphics.setColor(Color.DARK_GRAY);
bufferedImageGraphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
// now render a small image using java 2d with this text in it.
bufferedImageGraphics.setColor(Color.WHITE);
bufferedImageGraphics.drawString(problem, (bufferedImage.getWidth() - fontMetrics.stringWidth(problem)) / 2, (bufferedImage.getHeight() + fontMetrics.getAscent()) / 2);
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
pngImageData = byteArrayOutputStream.toByteArray();
} catch (IOException ioe) {
throw new IllegalStateException("unable to write png data in memory", ioe);
}
}
Captcha captcha = new Captcha();
captcha.setToken(UUID.randomUUID().toString());
captcha.setResponse(response);
captcha.setPngImageData(pngImageData);
return captcha;
}
Aggregations