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 : ký hiệu vật liệu trong bản vẽ xây dựng

<!- 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 : Cao xương ngựa: Ai nên dùng, ai không?

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

Hiểu về tiền thưởng và khuyến mãi tại các casino trực tuyến ở Việt Nam

Hiểu về tiền thưởng và khuyến mãi tại các casino trực tuyến ở Việt Nam

Ngành công nghiệp casino trực tuyến tại Việt Nam ngày càng phát triển mạnh mẽ, đi kèm với đó là hàng loạt chương trình tiền thưởng và…

Hướng Dẫn Nạp Tiền 789club tại pbglink.com

Hướng Dẫn Nạp Tiền 789club tại pbglink.com

Bạn đang tìm kiếm Hướng Dẫn Nạp Tiền 789Club một cách nhanh chóng và an toàn? Bài viết này sẽ giúp bạn thực hiện giao dịch nạp tiền vào…

Hướng dẫn cá cược thể thao cho người mới bắt đầu

Hướng dẫn cá cược thể thao cho người mới bắt đầu

Bạn muốn tìm hiểu mẹo cá cược thể thao tại Five88 chi tiết và lựa chọn nhà cái uy tín? Nhà cái Five88 là điểm đến lý…

Cách nạp tiền Rikvip nhanh chóng và an toàn nhất

Cách nạp tiền Rikvip nhanh chóng và an toàn nhất

Rikvip là một trong những nền tảng giải trí thu hút hàng triệu người chơi tuy nhiên, với những ai mới tham gia, việc thực hiện giao…

Cách chơi Poker tại Sky88 giúp bạn tăng tỷ lệ thắng

Cách chơi Poker tại Sky88 giúp bạn tăng tỷ lệ thắng

Poker là một trong những trò chơi bài hấp dẫn nhất thế giới, thu hút hàng triệu người tham gia nhờ vào sự kết hợp giữa kỹ…

Khám Phá Thế Giới Thể Thao Độc Đáo Cùng Thể Thao 789P – Tương Lai Của Giải Trí

Khám Phá Thế Giới Thể Thao Độc Đáo Cùng Thể Thao 789P – Tương Lai Của Giải Trí

Thể thao 789P là một nền tảng đang thu hút sự chú ý của đông đảo người yêu thể thao tại Việt Nam. Với nhiều dịch vụ…