3131import java .security .NoSuchAlgorithmException ;
3232import java .time .Duration ;
3333import java .time .Instant ;
34+ import java .util .Locale ;
3435import java .util .stream .Stream ;
3536
3637import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -74,8 +75,8 @@ void testGetTimeStep() throws NoSuchAlgorithmException {
7475 * different keys are used for each of the various HMAC algorithms.
7576 */
7677 @ ParameterizedTest
77- @ MethodSource ("totpTestVectorSource " )
78- void testGenerateOneTimePassword (final String algorithm , final Key key , final long epochSeconds , final int expectedOneTimePassword ) throws Exception {
78+ @ MethodSource ("argumentsForTestGenerateOneTimePasswordTotp " )
79+ void testGenerateOneTimePasswordTotp (final String algorithm , final Key key , final long epochSeconds , final int expectedOneTimePassword ) throws Exception {
7980
8081 final TimeBasedOneTimePasswordGenerator totp =
8182 new TimeBasedOneTimePasswordGenerator (Duration .ofSeconds (30 ), 8 , algorithm );
@@ -85,7 +86,7 @@ void testGenerateOneTimePassword(final String algorithm, final Key key, final lo
8586 assertEquals (expectedOneTimePassword , totp .generateOneTimePassword (key , timestamp ));
8687 }
8788
88- static Stream <Arguments > totpTestVectorSource () {
89+ private static Stream <Arguments > argumentsForTestGenerateOneTimePasswordTotp () {
8990 return Stream .of (
9091 arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 59L , 94287082 ),
9192 arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1111111109L , 7081804 ),
@@ -107,4 +108,76 @@ static Stream<Arguments> totpTestVectorSource() {
107108 arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 20000000000L , 47863826 )
108109 );
109110 }
111+
112+ @ ParameterizedTest
113+ @ MethodSource ("argumentsForTestGenerateOneTimePasswordStringTotp" )
114+ void testGenerateOneTimePasswordStringTotp (final String algorithm , final Key key , final long epochSeconds , final String expectedOneTimePassword ) throws Exception {
115+
116+ final TimeBasedOneTimePasswordGenerator totp =
117+ new TimeBasedOneTimePasswordGenerator (Duration .ofSeconds (30 ), 8 , algorithm );
118+
119+ final Instant timestamp = Instant .ofEpochSecond (epochSeconds );
120+
121+ assertEquals (expectedOneTimePassword , totp .generateOneTimePasswordString (key , timestamp ));
122+ }
123+
124+ private static Stream <Arguments > argumentsForTestGenerateOneTimePasswordStringTotp () {
125+ return Stream .of (
126+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 59L , "94287082" ),
127+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1111111109L , "07081804" ),
128+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1111111111L , "14050471" ),
129+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1234567890L , "89005924" ),
130+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 2000000000L , "69279037" ),
131+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 20000000000L , "65353130" ),
132+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 59L , "46119246" ),
133+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1111111109L , "68084774" ),
134+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1111111111L , "67062674" ),
135+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1234567890L , "91819424" ),
136+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 2000000000L , "90698825" ),
137+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 20000000000L , "77737706" ),
138+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 59L , "90693936" ),
139+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1111111109L , "25091201" ),
140+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1111111111L , "99943326" ),
141+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1234567890L , "93441116" ),
142+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 2000000000L , "38618901" ),
143+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 20000000000L , "47863826" )
144+ );
145+ }
146+
147+ @ ParameterizedTest
148+ @ MethodSource ("argumentsForTestGenerateOneTimePasswordStringLocaleTotp" )
149+ void testGenerateOneTimePasswordStringLocaleTotp (final String algorithm , final Key key , final long epochSeconds , final Locale locale , final String expectedOneTimePassword ) throws Exception {
150+
151+ final TimeBasedOneTimePasswordGenerator totp =
152+ new TimeBasedOneTimePasswordGenerator (Duration .ofSeconds (30 ), 8 , algorithm );
153+
154+ final Instant timestamp = Instant .ofEpochSecond (epochSeconds );
155+
156+ assertEquals (expectedOneTimePassword , totp .generateOneTimePasswordString (key , timestamp , locale ));
157+ }
158+
159+ private static Stream <Arguments > argumentsForTestGenerateOneTimePasswordStringLocaleTotp () {
160+ final Locale locale = Locale .forLanguageTag ("hi-IN" );
161+
162+ return Stream .of (
163+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 59L , locale , "९४२८७०८२" ),
164+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1111111109L , locale , "०७०८१८०४" ),
165+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1111111111L , locale , "१४०५०४७१" ),
166+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1234567890L , locale , "८९००५९२४" ),
167+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 2000000000L , locale , "६९२७९०३७" ),
168+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 20000000000L , locale , "६५३५३१३०" ),
169+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 59L , locale , "४६११९२४६" ),
170+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1111111109L , locale , "६८०८४७७४" ),
171+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1111111111L , locale , "६७०६२६७४" ),
172+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1234567890L , locale , "९१८१९४२४" ),
173+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 2000000000L , locale , "९०६९८८२५" ),
174+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 20000000000L , locale , "७७७३७७०६" ),
175+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 59L , locale , "९०६९३९३६" ),
176+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1111111109L , locale , "२५०९१२०१" ),
177+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1111111111L , locale , "९९९४३३२६" ),
178+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1234567890L , locale , "९३४४१११६" ),
179+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 2000000000L , locale , "३८६१८९०१" ),
180+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 20000000000L , locale , "४७८६३८२६" )
181+ );
182+ }
110183}
0 commit comments