Sha-1 Là Gì
SHA là gì?
SHA (Secure Hash Algorithm) là 1 trong những thuật toán băm dùng để làm chuyển một đoạn tài liệu nhất định thành một đoạn dữ liệu có chiều dài không đổi với xác suất khác biệt cao.
Bạn đang xem: Sha-1 là gì
Họ hàm băm SHA gồm: SHA-0, SHA-1, SHA-2, SHA-3
SHA0 không nhiều được áp dụng trên thực tế;SHA1 tương tự SHA0, tuy nhiên đã khắc phục một vài lỗi, chuỗi đầu ra output của SHA1 có kích cỡ 160 đậy và thương được màn trình diễn thành 40 số hexaSHA2 hạn chế lỗi của SHA1 và có nhiều thay đổi. Kích thước chuỗi đầu ra hoàn toàn có thể là 224, 256, 384 và 512 bít;SHA3 có thể chấp nhận được chuỗi cổng đầu ra có size không cố kỉnh định.SHA được sử dụng rộng rãi để bảo vệ tính chuẩn xác và toàn diện thông điệp.
Code ví dụ SHA
Để băm một đối tượng người dùng trong Java sử dụng SHA ta áp dụng class java.security.MessageDigest. Nó nhận đầu vào là một trong mảng byte và hiệu quả trả về là một trong mảng byte đã được băm.
Xem thêm: Ăn Sầu Riêng Ăn Với Gì Bị Ngộ Độc, Sầu Riêng Kiêng Ăn Với Gì
Tham số nguồn vào của method MessageDigest.getInstance sẽ khớp ứng với thuật toán băm, lấy một ví dụ SHA-1 (SHA1), SHA-224, SHA-256, SHA-384, SHA-512 (SHA2)
MessageDigest md = MessageDigest.getInstance("SHA-1");byte<> messageDigest = md.digest(dataInput);Ví dụ băm một quãng text đầu vào và hiển thị kết quả:
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class SHAHashing { public static void main(String<> args) throws Exception String password = "hawacorp.vn"; String hashedText = getSHAHash(password); System.out.println("Digest(in hex format): " + hashedText); public static String getSHAHash(String input) try MessageDigest md = MessageDigest.getInstance("SHA-1"); byte<> messageDigest = md.digest(input.getBytes()); return convertByteToHex(messageDigest); catch (NoSuchAlgorithmException e) throw new RuntimeException(e); public static String getSHAHash(File file) IOException e) e.printStackTrace(); throw new RuntimeException(e); public static String convertByteToHex(byte<> data) { StringBuffer sb = new StringBuffer(); for (int i = 0; i Kết quả:
Digest(in hex format): 26a4fcc7fd8a79f0a4de18e7881760f39bdc9a9dDemo áp dụng băm file, text bằng SHA1 – Java.
import java.awt.Color;import java.awt.EventQueue;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.UIManager;import javax.swing.border.EmptyBorder;import javax.swing.border.TitledBorder;public class MainApp extends JFrame private static final long serialVersionUID = 1L; private JPanel contentPane; private JPanel panelHashString; private JLabel lblInputText; private JTextArea textAreaResult; private JButton btnHashingText; private JTextArea textAreaInput; private JPanel panelHashFile; private JButton btnOpenFile; private JTextField textFieldFileUrl; private JButton btnHashingFile; private JTextArea textAreaFileHashing; private file file; /** * 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()); catch (Exception e) e.printStackTrace(); ); /** * Create the frame. */ public MainApp() setTitle("SHA Hashing - hawacorp.vn"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 680, 436); this.contentPane = new JPanel(); this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(this.contentPane); this.contentPane.setLayout(null); this.panelHashString = new JPanel(); this.panelHashString.setBorder(new TitledBorder(null, "Hashing String", TitledBorder.LEADING, TitledBorder.TOP, null, null)); this.panelHashString.setBounds(10, 11, 644, 187); this.contentPane.add(this.panelHashString); this.panelHashString.setLayout(null); this.lblInputText = new JLabel("Input Text:"); this.lblInputText.setBounds(10, 29, 93, 25); this.panelHashString.add(this.lblInputText); this.textAreaResult = new JTextArea(); this.textAreaResult.setLineWrap(true); this.textAreaResult.setBounds(385, 61, 249, 115); this.panelHashString.add(this.textAreaResult); this.btnHashingText = new JButton("Generate >>"); this.btnHashingText.setFont(UIManager.getFont("Button.font")); this.btnHashingText.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String đầu vào = textAreaInput.getText(); String result = SHAHashing.getSHAHash(input); textAreaResult.setText(result); ); this.btnHashingText.setBounds(269, 105, 108, 25); this.panelHashString.add(this.btnHashingText); this.textAreaInput = new JTextArea(); this.textAreaInput.setBounds(10, 61, 249, 115); this.panelHashString.add(this.textAreaInput); this.panelHashFile = new JPanel(); this.panelHashFile.setLayout(null); this.panelHashFile.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Hashing File", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); this.panelHashFile.setBounds(10, 209, 644, 187); this.contentPane.add(this.panelHashFile); this.btnOpenFile = new JButton("Open File"); this.btnOpenFile.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JFileChooser fileChooser = new JFileChooser(); int status = fileChooser.showOpenDialog(null); if (status == JFileChooser.APPROVE_OPTION) tệp tin = fileChooser.getSelectedFile(); textFieldFileUrl.setText(file.getAbsolutePath()); ); this.btnOpenFile.setBounds(10, 40, 116, 23); this.panelHashFile.add(this.btnOpenFile); this.textFieldFileUrl = new JTextField(); this.textFieldFileUrl.setBounds(136, 40, 330, 20); this.panelHashFile.add(this.textFieldFileUrl); this.textFieldFileUrl.setColumns(10); this.btnHashingFile = new JButton("Check SHA"); this.btnHashingFile.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String result = SHAHashing.getSHAHash(file); textAreaFileHashing.setText(result); ); this.btnHashingFile.setBounds(10, 84, 116, 23); this.panelHashFile.add(this.btnHashingFile); this.textAreaFileHashing = new JTextArea(); this.textAreaFileHashing.setBounds(136, 82, 330, 93); this.panelHashFile.add(this.textAreaFileHashing); Kết quả:

SHA là gì? Code lấy ví dụ như SHA1, SHA2 với Java
Okay, Done!
Download code lấy ví dụ như trên trên đây.
Xem thêm: Tìm Hiểu Các Vòng Gọi Vốn Của Startup: Series A / B / C Là Gì?
Referneces:
https://en.wikipedia.org/wiki/SHA-1
This entry was posted in Demo, Security và tagged security. Bookmark the permalink.