Skip to content
Snippets Groups Projects
Commit 8b3abd91 authored by Guy Zana's avatar Guy Zana
Browse files

cli: add md5sum util, used for validating downloads

parent 0c34d7f1
No related branches found
No related tags found
No related merge requests found
......@@ -115,4 +115,5 @@
/console/ifconfig.js: ../../console/ifconfig.js
/console/cli.js: ../../console/cli.js
/console/init.js: ../../console/init.js
/console/md5sum.js: ../../console/md5sum.js
/&/etc/hosts: ../../static/&
......@@ -15,6 +15,7 @@ load("/console/tests.js");
load("/console/run.js");
load("/console/ifconfig.js");
load("/console/arp.js");
load("/console/md5sum.js");
load("/console/route.js");
// Commands
......@@ -29,6 +30,7 @@ _commands["run"] = run_cmd;
_commands["ifconfig"] = ifconfig_cmd;
_commands["arp"] = arp_cmd;
_commands["route"] = route_cmd;
_commands["md5sum"] = md5sum_cmd;
// Create interface to networking functions
var networking_interface = new Networking();
......
var md5sum_cmd = {
init: function() {
this._md5 = new MD5();
},
md5sum: function(filename) {
var digest = this._md5.md5(cd.pwd() + "/" + filename);
return (digest)
},
invoke: function(inp) {
if (inp.length < 2) {
this.help();
return;
}
var digest = this.md5sum(inp[1]);
print(digest + " " + inp[1]);
return (digest);
},
tab: function(inp) {
if (inp.length > 2) {
return ([]);
}
if ((inp.length == 2) && (completed_word())) {
return ([]);
}
return (ls.ls());
},
help: function() {
print("md5sum: calculate md5 of file");
print("usage: md5sum <filename>");
}
}
package com.cloudius.util;
import java.io.FileInputStream;
import java.security.MessageDigest;
public class MD5 {
public static String toHex(byte[] bytes) {
final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char[] hexChars = new char[bytes.length * 2];
int v;
for ( int j = 0; j < bytes.length; j++ ) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static String md5(String filename) {
try {
FileInputStream fi = new FileInputStream(filename);
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
int chunk_sz = 0x1000;
byte bytes[] = new byte[chunk_sz];
int len = chunk_sz;
while (len == chunk_sz) {
len = fi.read(bytes);
m.update(bytes, 0, len);
}
fi.close();
// Calculate digest
byte[] digest = m.digest();
return (toHex(digest));
} catch (Exception ex){
System.out.println("md5() error:");
ex.printStackTrace();
return (null);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment