Time-Based One-Time Password (RFC 6238) and HMAC-Based One-Time Password (RFC 4226) reference implementations and more.

Overview

Crypto

Time-Based One-Time Password (RFC 6238) and HMAC-Based One-Time Password (RFC 4226) reference implementations and more.

Getting Started

TOTP generation

// Passwords will be 6 digits, change every 30 seconds, and are computed with SHA-1.
TOTP totp = new TOTP(6, Duration.ofSeconds(30L), Keys.generate("HmacSHA1", 160), SHA1.getInstance());

totp.compute(Instant.now());
// OR
totp.compute(Instant.now().toEpochMillis());

HOTP generation

// Passwords will be 6 digits and are computed with SHA-1.
HOTP hotp = new HOTP(6, Keys.generate("HmacSHA1", 160), SHA1.getInstance());

hotp.compute(0L);

HMAC signing

byte[] input = "Hello, World!".getBytes();

// Create an HMAC instance.
HMAC hmac = new HMAC(Keys.generate("AES"), SHA1.getInstance());

// Sign the input.
byte[] tag = hmac.sign(input);

...

// Verify the input later.
boolean valid = hmac.verify(input, tag);

Alternatively, you can use JceMAC, which wraps JCE's Mac:

JceMac hmac = new JceMac(Keys.generate("AES"), "HmacSHA1");

Hashing

Classes: MD2, MD5, SHA1, SHA224, SHA256, SHA384, SHA512, SHA512t224, SHA512t256.

byte[] input = "Hello, World!".getBytes();

// Hash the input using SHA-1.
byte[] hash = SHA1.getInstance().compute(input);

Utility classes' methods

Keys:

  • byte[] generate(String algorithm, SecureRandom secureRandom) – Generates a key, specifying the algorithm and SecureRandom.
  • byte[] generate(String algorithm, int keySize) – Generates a key, specifying the algorithm and key size in bytes.
  • byte[] generate(String algorithm) – Generates a key, specifying the algorithm.

Bytes:

  • byte[] concatenate(byte[] array, byte[]... arrays) – Concatenates n-arrays.
  • byte[] xor(byte[] array1, byte[] array2) – XOR operation on two arrays.
  • byte[] toHex(byte[] bytes) – Converts bytes to hexadecimal bytes.

License

This code is under the BSD 3-Clause.

Sponsoring

If you like my work and want to support it, please consider sponsoring me. Your support helps me make the time to code great things!

You might also like...

A Improved JByteMod Fork. Based on JByteMod-Reborn & JByteMod-Remastered.

A Improved JByteMod Fork. Based on JByteMod-Reborn & JByteMod-Remastered.

Jan 3, 2023

Messenger - A Java based project making use of Sockets for communication between the applications running on different JRE

Messenger - A Java based project making use of Sockets for communication between the applications running on different JRE. Multiple clients can connect at the same time and can send messages to each other, they also get the information of status of their friends connected to the server .

Jan 2, 2022

This App is a command line application, based on Cryptography.

This App is a command line application, based on Cryptography. where a user input a text and its encrypted and can be decrypted as well.

Jan 28, 2022

MARIOCASH: a trust-based multi-dimensional blockchains (branches)

MARIOCASH: a trust-based multi-dimensional blockchains (branches)

MARIOCASH is a trust-based multi-dimensional blockchains (branches) built with a vision to “Digitize everything into reality” and to connect everything and any blockchain networks.

Mar 10, 2022

Open Source Identity and Access Management For Modern Applications and Services

Keycloak Keycloak is an Open Source Identity and Access Management solution for modern Applications and Services. This repository contains the source

Jan 5, 2023

This application can recognize the sign language alphabets and help people who do not understand sign language to communicate with the speech and hearing impaired.

This application can recognize the sign language alphabets and help people who do not understand sign language to communicate with the speech and hearing impaired.

Sign Language Recognition App This application can recognize the sign language alphabets and help people who do not understand sign language to commun

Oct 7, 2021

JAP is an open source authentication middleware, it is highly decoupled from business code and has good modularity and flexiblity. Developers could integrate JAP into web applications effortlessly.

JAP is an open source authentication middleware, it is highly decoupled from business code and has good modularity and flexiblity. Developers could integrate JAP into web applications effortlessly.

🎨 JAP 是什么? JAP 是一款开源的登录中间件,基于模块化设计,并且与业务高度解耦,使用起来非常灵活,开发者可以毫不费力地将 JAP 集

Dec 1, 2022

Examples and HowTos for BouncyCastle and Java Cryptography Extension (JCE)

CryptographicUtilities Examples and HowTos for BouncyCastle and Java Cryptography Extension (JCE) See class "/src/main/java/de/soderer/utilities/crypt

Dec 19, 2021

Make a customized list of exercises, create and save workouts, and be led through your routine. This application is currently under development.

HIIT Workout Builder ABOUT This application allows you to create and be led through customized high-intensity interval training (HIIT) sessions. The a

Nov 28, 2022
Owner
Oliver Yasuna
Java and TypeScript developer with backend/cloud and frontend experience using AWS, Spring, Hibernate, Vaadin, Lit, and others.
Oliver Yasuna
A small and easy-to-use one-time password generator library for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP).

OTP-Java A small and easy-to-use one-time password generator for Java according to RFC 4226 (HOTP) and RFC 6238 (TOTP). Table of Contents Features Ins

Bastiaan Jansen 106 Dec 30, 2022
Password strength estimator

Nbvcxz - Password strength estimator - [] nbvcxz is java library (and standalone console program) which is heavily inspired by the work in zxcvbn. Pas

GoSimple 237 Dec 29, 2022
This is an android library to represent password strength.

PasswordStrengthView This is an android library to represent password strength. Preview How to use? Add maven to your project gradle file allprojects

null 33 Jan 3, 2022
evilzip lets you create a zip file(with password) that contains files with directory traversal characters in their embedded path.

evilzip logs 20210701 修改权限问题,让解压后的文件默认就有读写执行的权限。 About evilzip lets you create a zip file(with password) that contains files with directory traversal

鸭王 87 Dec 11, 2022
Trino UDFs Plugin to encrypt/decrypt values with a password

trino-encrypt-udfs Example of Trino UDFs Plugin to encrypt and decrypt values with a password. Introduction In Trino you can create new Plugins by imp

Victor Coustenoble 10 Dec 13, 2022
Repositório do curso Estruturas de Repetição e Arrays com Java. Curso este oferecido pela Digital Innovation one e ministrado por mim.

ESTRUTURAS DE REPETIÇÃO E ARRAYS COM JAVA Resolução dos exercícios propostos: ESTRUTURAS DE REPETIÇÃO E ARRAYS NA LINGUAGEM JAVA. Curso este que tive

Camila Cavalcante 994 Jan 5, 2023
Easily regenerate worlds at a specific time & date you want (SpigotMC plugin)

Restore/reset worlds at specific times without kicking players from the server! No need to go through the hassle of resetting your worlds manually anymore. Plenty of features are already included in the free version!

Kihsomray 11 Sep 23, 2022
Track your study time!

What's the time-tracker? The time-tracker is a nifty little tool to track the time you spent on your courses. Why does the time-tracker exist? This pr

null 8 Jun 29, 2022
Unixcounter - The new unixcounter project. Based on JavaEE + VueJS (server) and POSIX shell (client)

The Unix Counter Table of Contents Description Contributing Documentation Installation License This is the successor of the Linux Counter Project, whi

The Linux Counter Project 9 Dec 23, 2022
Java Project based on Java and Encryption using Cryptography algorithms

Symmetric-Encryption-Cryptography-in-Java Java Project based on Java and Encryption using Cryptography algorithms Project Aim Develop Java program to

Muhammad Asad 6 Feb 3, 2022