import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; public class PasswordEncoderTest { public static void main(String[] args) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String rawPassword = "mypassword"; String encodedPassword = encoder.encode(rawPassword); System.out.println(encodedPassword); } }
BCryptPasswordEncoder outputs a hashed password string that starts with a version prefix like "$2a$" or "$2b$" and is always 60 characters long. It is not plain text or numeric.
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; public class PasswordMatchTest { public static void main(String[] args) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String rawPassword = "secret123"; String encodedPassword = encoder.encode(rawPassword); boolean matches = encoder.matches("secret123", encodedPassword); System.out.println(matches); } }
The matches method returns true if the raw password matches the encoded hash. Since the same raw password is checked, it returns true.
Option D correctly defines a method annotated with @Bean that returns a new BCryptPasswordEncoder instance. Other options either miss @Bean, return type, or return statement.
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; public class PasswordCheck { public static void main(String[] args) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String rawPassword = "pass123"; String encodedPassword = encoder.encode(rawPassword); boolean matches = encoder.matches(rawPassword, rawPassword); System.out.println(matches); } }
The matches method expects the first argument as raw password and the second as encoded password. Here both are raw, so it returns false.
BCrypt adds a salt and is slow to compute, which makes it much harder for attackers to use brute-force or rainbow table attacks compared to fast hashes like MD5.