BCrypt là gì? Code ví dụ BCrypt bằng Java – JBCrypt

Cùng xem BCrypt là gì? Code ví dụ BCrypt bằng Java – JBCrypt trên youtube.

Bcrypt là gì? Code ví dụ BCrypt bằng Java – JBCrypt

(Xêm thêm: MD5 là gì? Code ví dụ MD5 với Java)

Bạn đang xem: Bcrypt là gì

(Xem thêm: SHA là gì? Code ví dụ SHA1, SHA2 với Java)

BCrypt là gì?

BCrypt là một thuật toán mã hóa mật khẩu được thiết kế bởi Niels Provos and David Mazières.

BCrypt được đánh giá là bảo mật và an toàn hơn so với MD5 và SHA bởi mỗi lần thực hiện băm nó lại cho một giá trị khác nhau, việc này khiến cho việc dò tìm mật khẩu trở nên khó hơn.

Code ví dụ BCrypt

Ở đây mình sử dụng thư viện JBCrypt để thực hiện băm và so sánh giá băm.

Xem thêm: No Mosaic Là Gì

Các bạn có thể download tư viện tại đây

Hoặc import bằng maven:

Xem Thêm : Năm nhuận là gì? Năm thường, năm nhuận có bao nhiêu ngày?

<!- Dongnaiart/artifact/org.mindrot/jbcrypt -> <dependency> <groupId>org.mindrot</groupId> <artifactId>jbcrypt</artifactId> <version>0.4</version> </dependency>

Ví dụ thực hiện băm một đoạn text:

String password = “stackjava.com”; String hash = Dongnaiartpw(password, Dongnaiartalt(12)); Dongnaiarttln(“BCrypt hash: ” + hash);

Trong đó Dongnaiartalt xác định số vòng, số vòng dao động từ 4-30, số vòng càng lớn thì thời gian thực hiện băm càng lâu.

Kết quả:

BCrypt hash: $2a$12$7oh8QTspcMvrD8zBjWW4NePLXm2Xf.ffFfXqj5CbLIf.elGx9A9Ce

Thực hiện chạy lại một lần nữa ta được một chuỗi khác:

BCrypt hash: $2a$12$Pmn0xKt4ZDtYAA6HypdWhuniNequ8ou2x7kG.9n.2Thw.Mza1RR.C

Xem thêm: Thủ đô của New Zealand là gì? Thông tin cơ bản về thủ đô New Zealand

Xem Thêm : Netizen Hàn phẫn nộ, tẩy chay cựu thành viên T-ara quay lại showbiz

Ví dụ thực hiện xác nhận mật khẩu với một đoạn mã băm (BCrypt hash)

String password = “stackjava.com”; boolean valuate = Dongnaiartkpw(password, “$2a$12$OFOICietLS3.qRtzIe6jE.vF.fmtL22DqIZ18WNMmQ.8nS7Frq5aO”); Dongnaiarttln(valuate);

Kết quả:

true

Demo mã hóa và xác nhận mật khẩu:

package Dongnaiart; import Dongnaiartr; import DongnaiarttQueue; import DongnaiartonEvent; import DongnaiartonListener; import DongnaiartultComboBoxModel; import Dongnaiartton; import DongnaiartboBox; import Dongnaiartme; import Dongnaiartel; import Dongnaiartel; import DongnaiarttField; import DongnaiartgUtilities; import Dongnaiartnager; import DongnaiartyBorder; import DongnaiartedBorder; import Dongnaiartpt; 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) { DongnaiartkeLater(new Runnable() { public void run() { try { MainApp frame = new MainApp(); Dongnaiartisible(true); DongnaiartookAndFeel(UIManager.getSystemLookAndFeelClassName()); DongnaiartteComponentTreeUI(frame); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MainApp() { setTitle(“BCrypt Demo – Dongnaiart”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 579, 399); DongnaiartentPane = new JPanel(); Dongnaiartorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(this.contentPane); Dongnaiartayout(null); DongnaiartlCalculator = new JPanel(); Dongnaiartorder(new TitledBorder(null, “BCrypt Calculator”, DongnaiartING, Dongnaiart, null, null)); Dongnaiartounds(12, 13, 543, 142); Dongnaiart(this.panelCalculator); Dongnaiartayout(null); DongnaiartnterPasswordTo = new JLabel(“Enter password to Hash: “); Dongnaiartounds(12, 28, 152, 16); Dongnaiart(this.lblEnterPasswordTo); DongnaiartFieldPassToHash = new JTextField(); Dongnaiartounds(153, 25, 372, 22); Dongnaiart(this.textFieldPassToHash); Dongnaiartolumns(10); Dongnaiartound = new JLabel(“Maximum number of rounds which is tolerable:”); Dongnaiartounds(12, 61, 278, 16); Dongnaiart(this.lblRound); DongnaiartoBoxRound = new JComboBox(); Dongnaiartodel(new DefaultComboBoxModel(new String[] {“4”, “5”, “6”, “7”, “8”, “9”, “10”, “11”, “12”, “13”, “14”, “15”})); Dongnaiartounds(353, 57, 172, 22); Dongnaiart(this.comboBoxRound); DongnaiartalculateHash = new JButton(“Calculate”); DongnaiartctionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String password = Dongnaiartext(); if (password != null && !password.isEmpty()) { int round = DongnaiarteInt(comboBoxRound.getSelectedItem().toString()); String hash = Dongnaiartpw(password, Dongnaiartalt(round)); Dongnaiartext(hash); } } }); Dongnaiartounds(12, 90, 95, 25); Dongnaiart(this.btnCalculateHash); DongnaiartFieldHashResult = new JTextField(); Dongnaiartditable(false); Dongnaiartounds(153, 90, 372, 22); Dongnaiart(this.textFieldHashResult); Dongnaiartolumns(10); DongnaiartlTester = new JPanel(); Dongnaiartorder(new TitledBorder(UIManager.getBorder(“TitledBorder.border”), “BCrypt Tester”, DongnaiartING, Dongnaiart, null, new Color(0, 0, 0))); Dongnaiartounds(12, 179, 543, 175); Dongnaiart(this.panelTester); Dongnaiartayout(null); DongnaiartasswordToEvaluate = new JLabel(“Password to evaluate:”); Dongnaiartounds(12, 26, 134, 16); Dongnaiart(this.lblPasswordToEvaluate); DongnaiartFieldPassToEvaluate = new JTextField(); Dongnaiartolumns(10); Dongnaiartounds(154, 23, 380, 22); Dongnaiart(this.textFieldPassToEvaluate); DongnaiartFieldBcryptHashToEvaluate = new JTextField(); Dongnaiartolumns(10); Dongnaiartounds(154, 74, 380, 22); Dongnaiart(this.textFieldBcryptHashToEvaluate); DongnaiartcryptHashTo = new JLabel(“BCrypt hash to evaluate:”); Dongnaiartounds(12, 77, 134, 16); Dongnaiart(this.lblBcryptHashTo); Dongnaiartest = new JButton(“Calculate”); DongnaiartctionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String password = Dongnaiartext(); String hash = Dongnaiartext(); boolean match = Dongnaiartkpw(password, hash); if (match) { Dongnaiartext(“Password and hash match”); } else { Dongnaiartext(“Password and hash do NOT match”); } } }); Dongnaiartounds(12, 123, 95, 25); Dongnaiart(this.btnTest); DongnaiartFieldEvaluteResult = new JTextField(); Dongnaiartditable(false); Dongnaiartolumns(10); Dongnaiartounds(153, 123, 372, 22); Dongnaiart(this.textFieldEvaluteResult); } }

BCrypt là gì? Code ví dụ BCrypt bằng Java - JBCrypt

BCrypt là gì? Code ví dụ BCrypt bằng Java – JBCrypt

Okay, Done!

Download code ví dụ trên tại đây.

References:

Có thể bạn quan tâm: Hướng dẫn đặt mật khẩu cho ứng dụng Zalo

Nguồn: https://dongnaiart.edu.vn
Danh mục: Tin tức

Lời kết: Trên đây là bài viết BCrypt là gì? Code ví dụ BCrypt bằng Java – JBCrypt. Hy vọng với bài viết này bạn có thể giúp ích cho bạn trong cuộc sống, hãy cùng đọc và theo dõi những bài viết hay của chúng tôi hàng ngày trên website: Dongnaiart.edu.vn

Related Posts

Top 5 nhà cái giao dịch rút tiền nhanh chóng và hiệu quả

Top 5 nhà cái giao dịch rút tiền nhanh chóng và hiệu quả

Trong thế giới cá cược trực tuyến, việc chọn lựa nhà cái có dịch vụ giao dịch rút tiền nhanh chóng và hiệu quả là một yếu…

Bíp kíp bắt kèo đá gà trực tiếp uy tín, đơn giản cho kê thủ

Bíp kíp bắt kèo đá gà trực tiếp uy tín, đơn giản cho kê thủ

Các trang cá cược ngày nay đã phát triển một loạt các kèo đá gà trực tiếp, mang lại sự đa dạng và cơ hội kiếm tiền…

Cách lựa chọn nhà cái trực tuyến uy tín và chất lượng

Cách lựa chọn nhà cái trực tuyến uy tín và chất lượng

Khi quyết định tham gia cá cược trực tuyến, việc lựa chọn một nhà cái uy tín và chất lượng là điều vô cùng quan trọng. Trên…

Cược Đá Gà HB88 Và Những Ưu Đãi Ngập Tràn Tại Nhà Cái 

Cược Đá Gà HB88 Và Những Ưu Đãi Ngập Tràn Tại Nhà Cái 

Sảnh đá gà HB88 được xem là một trong những điểm đến cá cược hấp dẫn, mang đến những trận đấu đầy kịch tính, căng thẳng. Trong…

Roulette là gì? Kinh nghiệm chơi Roulette luôn thắng từ cao thủ

Roulette là gì? Kinh nghiệm chơi Roulette luôn thắng từ cao thủ

Roulette là gì? Roulette là một trò chơi sòng bạc phổ biến được chơi trên một bàn quay có chứa một bánh xe quay và một bảng…

Giải thích kèo chấp 1.5 – Các mẹo chơi hiệu quả 

Giải thích kèo chấp 1.5 – Các mẹo chơi hiệu quả 

Bóng đá được mệnh môn thể thao vua, là niềm đam mê của không ít người hâm mộ trên toàn thế giới. Kèo chấp 1.5 trong bóng…