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 : Khắc phục lỗi Unknown USB Device với 4 cách đơn giản

<!- 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 : Văn mẫu lớp 10: Phân tích nhân vật Tấm trong truyện Tấm Cám Dàn ý & 14 bài phân tích nhân vật Tấm

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

Kèo châu Á – Tìm Hiểu Chi Tiết Về Một Hình Thức Cá Cược Phổ Biến

Kèo châu Á – Tìm Hiểu Chi Tiết Về Một Hình Thức Cá Cược Phổ Biến

Kèo châu Á là một trong những hình thức cá cược phổ biến nhất hiện nay, đặc biệt là trong lĩnh vực thể thao. Bài viết này Bet88 sẽ…

Nổ hũ hấp dẫn người chơi – Thế giới sắc màu của cơ hội và chiến thắng

Nổ hũ hấp dẫn người chơi – Thế giới sắc màu của cơ hội và chiến thắng

Nổ hũ hấp dẫn người chơi không chỉ là một trò chơi may rủi mà còn là một nghệ thuật. Với những âm thanh vui nhộn, hình…

Thế Giới Bắn Cá New88 – Trải Nghiệm Độc Đáo Từ Game Đến Thực Tế

Thế Giới Bắn Cá New88 – Trải Nghiệm Độc Đáo Từ Game Đến Thực Tế

Có thể bạn quan tâm công thức số tiền bằng chữ Tổng hợp 9999+ ảnh gái xinh dễ thương và lạnh lùng – Invert.vn Windows Recovery Environment…

Bắn Cá Phần Thưởng Cao – Trải Nghiệm Giải Trí Đỉnh Cao Cho Game Thủ

Bắn Cá Phần Thưởng Cao – Trải Nghiệm Giải Trí Đỉnh Cao Cho Game Thủ

Bắn cá phần thưởng cao đang trở thành một trò chơi phổ biến thu hút hàng triệu người chơi. Trò chơi không chỉ mang lại giây phút…

Xổ số QQ88 – Chơi ngay và nhận thưởng lớn!

Xổ số QQ88 – Chơi ngay và nhận thưởng lớn!

Xổ số QQ88 là một trong những hình thức giải trí được ưa chuộng nhất hiện nay, không chỉ bởi sự hấp dẫn từ việc trúng thưởng…

Xổ Số W88: Mô Hình Cược Mới Mẻ và Lợi Nhuận Khủng

Xổ Số W88: Mô Hình Cược Mới Mẻ và Lợi Nhuận Khủng

Xổ số W88 đã khiến cả cộng đồng game thủ và những người đam mê cá cược chao đảo với mô hình cược cực kỳ đa dạng và…