echo will normally output a newline, which is suppressed with -n. Try this:

echo -n foobar | sha256sum