SHA là gì?

SHA (Secure Hash Algorithm) là một thuật toán băm dùng để chuyển một đoạn dữ 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: Sha1 là gì

Họ hàm băm SHA gồm: SHA-0, SHA-1, SHA-2, SHA-3

SHA0 ít được sử dụng trên thực tế;SHA1 tương tự SHA0, nhưng đã khắc phục một số lỗi, chuỗi đầu ra của SHA1 có kích thước 160 bít và thương được biểu diễn thành 40 số hexaSHA2 khắc phục lỗi của SHA1 và có nhiều thay đổi. Kích thước chuỗi đầu ra có thể là 224, 256, 384 và 512 bít;SHA3 cho phép chuỗi đầu ra có kích thước không cố định.

SHA được sử dụng rộng rãi để đảm bảo tính xác thực và toàn vẹn thông điệp.

Code ví dụ SHA

Để băm một đối tượng trong Java sử dụng SHA ta sử dụng class java.security.MessageDigest. Nó nhận đầu vào là một mảng byte và kết quả trả về là một mảng byte đã được băm.

Xem thêm: Thuật Ngữ Là Gì – Thuật Ngữ Sách Giáo Khoa Ngữ Văn Lớp 9

Tham số đầu vào của method MessageDigest.getInstance sẽ tương ứng với thuật toán băm, 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 đoạn 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 = “thienmaonline.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) { MessageDigest md; try { md = MessageDigest.getInstance(“SHA-1”); FileInputStream fis = new FileInputStream(file); byte dataBytes = new byte; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); } byte byteData = md.digest(); fis.close(); return convertByteToHex(byteData); } catch (NoSuchAlgorithmException | 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 ứng 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 – thienmaonline.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 input = 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) { file = 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 ví dụ SHA1, SHA2 với Java

Okay, Done!

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

Xem thêm: Blw Là Gì – Làm Cách Nào Để Giúp Bé Tự Ăn

Referneces:

https://en.wikipedia.org/wiki/SHA-1

This entry was posted in Demo, Security and tagged security. Bookmark the permalink.

Điều hướng bài viết

Chuyên mục: Hỏi Đáp