Initialize Generator
The secret generator is used to generate secrets in Base32 encoding that can be used with the various OTP (One-Time Password) generators.
Create secret generator
You can create an instance of the SecretGenerator in the following way:
val base32Secret: Base32Secret = secretGenerator.generateSecret()
Spring Boot
Instead of creating a new instance of a generator each time a token is checked, it is also possible to create a bean within Spring. This allows to configure the generator once and this configuration is maintained each time the bean is injected into a component.
@Bean
fun secretGenerator(): SecretGenerator {
val generator = SecretGenerator()
generator.randomGenerator = RandomGenerator(charPool = listOf('B', 'C', 'D'))
return generator
}
This bean can then be injected in the constructor of any class marked with @Component (@Service, ...).
@Component
class CustomComponent(private val secretGenerator: SecretGenerator) {
//...
}
Customize properties
It is possible to customize the properties of the generator, either by setters or applying them in the constructor.
Code length
The code length specifies how long a generated code will be. If the code length is changed, it is necessary that the user's authenticator app supports this as well.
Most authenticator apps, such as those from Microsoft or Google, use a length of 6 digits. If you change this number, they are no longer usable. However, remember that that they are widely used before you drop support for them.