BCrypt là gì?
BCrypt là một trong những thuật toán mã hóa mật khẩu đăng nhập có phong cách thiết kế bởi Niels Provos and David Mazières.
BCrypt được review là bảo mật thông tin với bình yên hơn so với MD5 cùng SHA bởi vì mỗi lần tiến hành băm này lại cho 1 cực hiếm không giống nhau, câu hỏi này làm cho câu hỏi dò tìm kiếm password trngơi nghỉ phải nặng nề rộng.
Xem thêm: Adobe Photoshop Elements Là Gì, Photoshop Elements Là Gì
Code ví dụ BCrypt
Ở trên đây bản thân sử dụng thỏng viện JBCrypt nhằm tiến hành băm và đối chiếu giá băm.
Các bạn cũng có thể tải về bốn viện tại đây
Hoặc import bằng maven:
org.mindrot jbcrypt 0.4Ví dụ tiến hành băm một quãng text:
String password = "infobandarpkr.com";String hash = BCrypt.hashpw(password, BCrypt.gensalt(12));System.out.println("BCrypt hash: " + hash);Trong đó BCrypt.gensalt xác minh số vòng, số vòng dao động từ 4-30, số vòng càng mập thì thời hạn triển khai băm càng thọ.
Kết quả:
BCrypt hash: $2a$12$7oh8QTspcMvrD8zBjWW4NePLXm2Xf.ffFfXqj5CbLIf.elGx9A9CeThực hiện chạy lại một đợt tiếp nhữa ta được một chuỗi khác:
BCrypt hash: $2a$12$Pmn0xKt4ZDtYAA6HypdWhuniNequ8ou2x7kG.9n.2Thw.Mza1RR.Clấy một ví dụ triển khai xác nhận mật khẩu với một quãng mã băm (BCrypt hash)
String password = "infobandarpkr.com";boolean valuate = BCrypt.checkpw(password, "$2a$12$OFOICietLS3.qRtzIe6jE.vF.fmtL22DqIZ18WNMmQ.8nS7Frq5aO");System.out.println(valuate);Kết quả:
true
Demo mã hóa với xác nhận mật khẩu:
package infobandarpkr.com.bcryptthử nghiệm.demo;import java.awt.Color;import java.awt.EventQueue;import java.awt.sự kiện.ActionEvent;import java.awt.event.ActionListener;import javax.swing.DefaultComboBoxModel;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.border.EmptyBorder;import javax.swing.border.TitledBorder;import org.mindrot.jbcrypt.BCrypt;public class MainApp extends JFrame private static final long serialVersionUID = 1L; private JPanel contentPane; private JPanel panelCalculator; private JPanel panelTester; private JLabel lblEnterPasswordTo; private JTextField textFieldPassToHash; private JLabel lblRound; private JComboBox comboBoxRound; private JButton btnCalculateHash; private JTextField textFieldHashResult; private JLabel lblPasswordToEvaluate; private JTextField textFieldPassToEvaluate; private JTextField textFieldBcryptHashToEvaluate; private JLabel lblBcryptHashTo; private JButton btnTest; private JTextField textFieldEvaluteResult; /** * Launch the application. */ public static void main(String<> args) EventQueue.invokeLater(new Runnable() public void run() try MainApp frame = new MainApp(); frame.setVisible(true); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(frame); catch (Exception e) e.printStackTrace(); ); /** * Create the frame. */ public MainApp() setTitle("BCrypt Demo - infobandarpkr.com"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 579, 399); this.contentPane = new JPanel(); this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(this.contentPane); this.contentPane.setLayout(null); this.panelCalculator = new JPanel(); this.panelCalculator.setBorder(new TitledBorder(null, "BCrypt Calculator", TitledBorder.LEADING, TitledBorder.TOP, null, null)); this.panelCalculator.setBounds(12, 13, 543, 142); this.contentPane.add(this.panelCalculator); this.panelCalculator.setLayout(null); this.lblEnterPasswordTo = new JLabel("Enter password khổng lồ Hash: "); this.lblEnterPasswordTo.setBounds(12, 28, 152, 16); this.panelCalculator.add(this.lblEnterPasswordTo); this.textFieldPassToHash = new JTextField(); this.textFieldPassToHash.setBounds(153, 25, 372, 22); this.panelCalculator.add(this.textFieldPassToHash); this.textFieldPassToHash.setColumns(10); this.lblRound = new JLabel("Maximum number of rounds which is tolerable:"); this.lblRound.setBounds(12, 61, 278, 16); this.panelCalculator.add(this.lblRound); this.comboBoxRound = new JComboBox(); this.comboBoxRound.setModel(new DefaultComboBoxModel(new String<> "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15")); this.comboBoxRound.setBounds(353, 57, 172, 22); this.panelCalculator.add(this.comboBoxRound); this.btnCalculateHash = new JButton("Calculate"); this.btnCalculateHash.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String password = textFieldPassToHash.getText(); if (password != null && !password.isEmpty()) int round = Integer.parseInt(comboBoxRound.getSelectedItem().toString()); String hash = BCrypt.hashpw(password, BCrypt.gensalt(round)); textFieldHashResult.setText(hash); ); this.btnCalculateHash.setBounds(12, 90, 95, 25); this.panelCalculator.add(this.btnCalculateHash); this.textFieldHashResult = new JTextField(); this.textFieldHashResult.setEditable(false); this.textFieldHashResult.setBounds(153, 90, 372, 22); this.panelCalculator.add(this.textFieldHashResult); this.textFieldHashResult.setColumns(10); this.panelTester = new JPanel(); this.panelTester.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "BCrypt Tester", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); this.panelTester.setBounds(12, 179, 543, 175); this.contentPane.add(this.panelTester); this.panelTester.setLayout(null); this.lblPasswordToEvaluate = new JLabel("Password lớn evaluate:"); this.lblPasswordToEvaluate.setBounds(12, 26, 134, 16); this.panelTester.add(this.lblPasswordToEvaluate); this.textFieldPassToEvaluate = new JTextField(); this.textFieldPassToEvaluate.setColumns(10); this.textFieldPassToEvaluate.setBounds(154, 23, 380, 22); this.panelTester.add(this.textFieldPassToEvaluate); this.textFieldBcryptHashToEvaluate = new JTextField(); this.textFieldBcryptHashToEvaluate.setColumns(10); this.textFieldBcryptHashToEvaluate.setBounds(154, 74, 380, 22); this.panelTester.add(this.textFieldBcryptHashToEvaluate); this.lblBcryptHashTo = new JLabel("BCrypt hash lớn evaluate:"); this.lblBcryptHashTo.setBounds(12, 77, 134, 16); this.panelTester.add(this.lblBcryptHashTo); this.btnTest = new JButton("Calculate"); this.btnTest.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String password = textFieldPassToEvaluate.getText(); String hash = textFieldBcryptHashToEvaluate.getText(); boolean match = BCrypt.checkpw(password, hash); if (match) textFieldEvaluteResult.setText("Password và hash match"); else textFieldEvaluteResult.setText("Password và hash bởi vì NOT match"); ); this.btnTest.setBounds(12, 123, 95, 25); this.panelTester.add(this.btnTest); this.textFieldEvaluteResult = new JTextField(); this.textFieldEvaluteResult.setEditable(false); this.textFieldEvaluteResult.setColumns(10); this.textFieldEvaluteResult.setBounds(153, 123, 372, 22); this.panelTester.add(this.textFieldEvaluteResult);