0
\$\begingroup\$

I need to get text hash (sha 256), so I need to convert file to its type representation. Is this a fast way to get a byte representation of a file?

public byte[] bEad(String FILENAME) {
 RandomAccessFile f=null;
 try {
 f=new RandomAccessFile(FILENAME, "r");
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 }
 byte[] b=new byte[0];
 try {
 b=new byte[(int) f.length()];
 } catch (IOException e) {
 e.printStackTrace();
 }
 try {
 f.readFully(b);
 f.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return b;
}
MessageDigest name=MessageDigest.getInstance("SHA-256");
name.update(bEad("D:\crytp.txt"))
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Sep 25, 2017 at 16:32
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Have you tested this code? Does it work for you? It sounds like you are unsure about what your code does. \$\endgroup\$ Commented Sep 25, 2017 at 17:06
  • \$\begingroup\$ it's working and run fast, (I share my solution). \$\endgroup\$ Commented Sep 25, 2017 at 17:19
  • \$\begingroup\$ Can you please show your real code, where you are calling the name.update and instantiating the MessageDigest? \$\endgroup\$ Commented Sep 25, 2017 at 18:05
  • \$\begingroup\$ Do you want to get feedback on your code, or what exactly is your purpose of posting this here? \$\endgroup\$ Commented Sep 25, 2017 at 18:05
  • \$\begingroup\$ I need a feedback \$\endgroup\$ Commented Sep 25, 2017 at 18:31

1 Answer 1

2
\$\begingroup\$

If have no idea what bEad means, or why your function is named that way. Also, it should probably be a static method, since it uses no object state.

By convention, all-caps names, like FILENAME, should be used only for constants.

Your error handling is bad. Suppose the file does not exist... then what? You'll get a NullPointerException after printing a stack trace. The correct thing to do is not to catch any of the exceptions. Just declare that the method throws IOException.

This code could be expressed more succinctly using Files.readAllBytes(): see this Stack Overflow answer:

byte[] b = Files.readAllBytes(Paths.get(filename));
byte[] hash = MessageDigest.getInstance("SHA-256").digest(b);

Note, however, that that solution, as well as your, scales poorly for large files, since you read the entire file into memory. It would be better to compute the message digest by reading, say, 8192 bytes at a time.

answered Sep 25, 2017 at 20:43
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.