Skip to content

Commit 9c71576

Browse files
authored
Add Argon2 Java snippet (#664)
1 parent ddcbe66 commit 9c71576

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/test/java/com/google/firebase/snippets/FirebaseAuthSnippets.java

+32
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
import com.google.firebase.auth.UserRecord;
3838
import com.google.firebase.auth.UserRecord.CreateRequest;
3939
import com.google.firebase.auth.UserRecord.UpdateRequest;
40+
import com.google.firebase.auth.hash.Argon2;
41+
import com.google.firebase.auth.hash.Argon2.Argon2HashType;
42+
import com.google.firebase.auth.hash.Argon2.Argon2Version;
4043
import com.google.firebase.auth.hash.Bcrypt;
4144
import com.google.firebase.auth.hash.HmacSha256;
4245
import com.google.firebase.auth.hash.Pbkdf2Sha256;
@@ -588,6 +591,35 @@ public void importWithScrypt() {
588591
// [END import_with_scrypt]
589592
}
590593

594+
public void importWithArgon2() {
595+
// [START import_with_argon2]
596+
try {
597+
List<ImportUserRecord> users = Collections.singletonList(ImportUserRecord.builder()
598+
.setUid("some-uid")
599+
.setEmail("[email protected]")
600+
.setPasswordHash("password-hash".getBytes())
601+
.setPasswordSalt("salt".getBytes())
602+
.build());
603+
UserImportOptions options = UserImportOptions.withHash(
604+
Argon2.builder()
605+
.setHashLengthBytes(512)
606+
.setHashType(Argon2HashType.ARGON2_ID)
607+
.setParallelism(8)
608+
.setIterations(16)
609+
.setMemoryCostKib(2048)
610+
.setVersion(Argon2Version.VERSION_10)
611+
.setAssociatedData("associated-data".getBytes())
612+
.build());
613+
UserImportResult result = FirebaseAuth.getInstance().importUsers(users, options);
614+
for (ErrorInfo indexedError : result.getErrors()) {
615+
System.out.println("Failed to import user: " + indexedError.getReason());
616+
}
617+
} catch (FirebaseAuthException e) {
618+
System.out.println("Error importing users: " + e.getMessage());
619+
}
620+
// [END import_with_argon2]
621+
}
622+
591623
public void importWithoutPassword() {
592624
// [START import_without_password]
593625
try {

0 commit comments

Comments
 (0)