Tuesday, March 20, 2012

Better checksum in Java

You were also pissed off by the need of writing shitcode like you can find on RoseIndia examples while striving to get a way to represent digest bytes as a hexadecimal string, right? Here is a better way:
MessageDigest digest = MessageDigest.getInstance("SHA1"); // Or MD5...
digest.update("hello world");
String hexdigest = new BigInteger(1, digesst.digest()).toString(16);
And that's it.

Wednesday, March 07, 2012

Want to reset Linux machine remotely?

Sometimes it is a good idea to completely reset the machine without shutting it down gracefully. Especially, doing it remotely via SSH, for example.

Here is how:

$ sudo -s
# echo 1 > /proc/sys/kernel/panic
# echo c > /proc/sysrq-trigger

The file "panic" by default has value "0". If you want machine actually reboot, this needs to be changed, otherwise machine will simply "hang" until hardware reset or power cycle.

The file "sysrq-trigger" has a lot of commands, where "c" is what we need at this time. Here is full list of the commands one could use in a future:
  • 'r' - Turns off keyboard raw mode and sets it to XLATE.
  • 'k' - Secure Access Key. Kills all programs on the current virtual console.
  • 'b' - Immediately reboot the system without syncing or unmounting disks.
  • 'c' - Intentionally crash the system without syncing or unmounting disks. This is most useful if the NETDUMP client package has been installed.
  • 'o' - Shut the system off (if configured and supported).
  • 's' - Attempt to sync all mounted filesystems.
  • 'u' - Attempt to remount all mounted filesystems read-only.
  • 'p' - Dump the current registers and flags to current console.
  • 't' - Dump a list of current tasks and their information to current console.
  • 'm' - Dump current memory info to current console.
  • '0'-'9' - Sets the console log level, controlling which kernel messages will be printed to current console. For example, '0' would make it so that only emergency messages like PANICs or OOPSes would make it to current console.
  • 'e' - Send a SIGTERM to all processes, except for init.
  • 'i' - Send a SIGKILL to all processes, except for init.
  • 'l' - Send a SIGKILL to all processes, INCLUDING init. This turns the system completely hosed.
Hope it helps.