up
Hash
Available algorithm
| command: | ns_mhash hash algorithms |
| result: | list of available hash algorithms |
| example: | [ns_mhash hash algorithms]=<%=[ns_mhash hash algorithms]%> |
create new HASH object
| command: | ns_mhash hash new algorithm_name |
| parameters: | algorithm_name - name of hash algorithm: MD5, SHA1, ... |
| result: | HASH object using in other commands |
| example: | [ns_mhash hash new MD5] |
destroy HASH object
| command: | ns_mhash hash destroy hash_id |
| parameters: | hash_id - id of HASH object generated by new command (above) |
| result: | destroy HASH object without any output |
| example: | ns_mhash hash destroy $hashid |
end/print HASH object
| command: | ns_mhash hash end ?-bin|-base64? hash_id |
| parameters: |
hash_id - id of HASH object generated by new command (above)
?-bin|-base64? - type of output format for generated hash string :: default: hexadecimal string; binary (TclByteArray): -bin; binary base64 encoded: -base64
|
| result: | return output hash string and destroy HASH object |
| example: | set out [ns_mhash hash end -base64 $hashid] |
copy HASH object
| command: | ns_mhash hash copy hash_id |
| parameters: | hash_id - id of HASH object generated by new command (above) |
| result: | copy of HASH object |
| example: | set hashid2 [ns_mhash hash copy $hashid] |
update HASH object
| command: | ns_mhash hash update hash_id string |
| parameters: | hash_id - id of HASH object generated by new command (above)
string - string you want to hash |
| result: | update HASH object by string. You can use this repeatedly |
| example: | ns_mhash hash update $hashid "Hello world" |
detach HASH object
| command: | ns_mhash hash detach hash_id |
| parameters: | hash_id - id of HASH object generated by new command (above) |
| result: | detach HASH object from Tcl interpreter. You can you with nsv_* for share HASH object. |
| example: |
set detachid [ns_mhash hash detach $hashid]
nsv_set test test $detachid
|
attach HASH object
| command: | ns_mhash hash attach detach_hash_id |
| parameters: | detach_hash_id - detach id of HASH object generated by detach command (above) |
| result: | attach HASH object to Tcl interpreter for next using. You can you with nsv_* for share HASH object. |
| example: |
set detachid [nsv_get test test]
set hashid [ns_mhash hash attach $detachid]
|
serialize HASH object
| command: | ns_mhash hash serialize hash_id |
| parameters: | hash_id - id of HASH object generated by new command (above) |
| result: | serialize HASH object and destroy this HASH object. The result is base64 encoded HASH object. You can you with nsv_* for share HASH object or save to file|database. |
| example: |
set serializeobj [ns_mhash hash serialize $hashid]
nsv_set test test $serializeobj
|
unserialize HASH object
| command: | ns_mhash hash unserialize serialize_hash_str |
| parameters: | serialize_hash_str - serialize HASH object generated by serialize command (above) |
| result: | create HASH object from serialize string. You can you with nsv_* for share HASH object or save to file|database. |
| example: |
set serializeobj [nsv_get test test]
set hashid [ns_mhash hash unserialize $serializeobj]
|
Full samples:
simple without output
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello world"
ns_mhash hash destroy $td
simple with hash string (base64)
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello world"
set out [ns_mhash hash end -base64 $td]
ns_adp_puts $out
result: <%
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello world"
set out [ns_mhash hash end -base64 $td]
ns_adp_puts $out
%>
simple with hash string (hexadecimal)
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello world"
set out [ns_mhash hash end $td]
ns_adp_puts $out
result: <%
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello world"
set out [ns_mhash hash end $td]
ns_adp_puts $out
%>
copy of HASH object
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello"
set td2 [ns_mhash hash copy $td]
set out [ns_mhash hash end $td]
ns_mhash hash update $td2 " "
ns_mhash hash update $td2 "world"
set out2 [ns_mhash hash end $td2]
ns_adp_puts $out
ns_adp_puts $out2
<%
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello"
set td2 [ns_mhash hash copy $td]
set out [ns_mhash hash end $td]
ns_mhash hash update $td2 " "
ns_mhash hash update $td2 "world"
set out2 [ns_mhash hash end $td2]
%>
result out: <%=$out%>
result out2: <%=${out2}%>
attach/detach of HASH object
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello"
set detachid [ns_mhash hash detach $td]
nsv_set test test $detachid
set detachid [nsv_get test test]
set td [ns_mhash hash attach $detachid]
ns_mhash hash update $td " "
ns_mhash hash update $td "world"
set out [ns_mhash hash end $td]
ns_adp_puts $out
<%
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello"
set detachid [ns_mhash hash detach $td]
nsv_set test test $detachid
set detachid [nsv_get test test]
set td [ns_mhash hash attach $detachid]
ns_mhash hash update $td " "
ns_mhash hash update $td "world"
set out [ns_mhash hash end $td]
%>
result: <%=$out%>
serialize/unserialize of HASH object
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello"
set serializestr [ns_mhash hash serialize $td]
nsv_set test test $serializestr
set serializestr [nsv_get test test]
set td [ns_mhash hash unserialize $serializestr]
ns_mhash hash update $td " world"
set out [ns_mhash hash end $td]
ns_adp_puts $out
<%
set td [ns_mhash hash new SHA1]
ns_mhash hash update $td "Hello"
set serializestr [ns_mhash hash serialize $td]
nsv_set test test $serializestr
set serializestr [nsv_get test test]
set td [ns_mhash hash unserialize $serializestr]
ns_mhash hash update $td " world"
set out [ns_mhash hash end $td]
%>
result serialize HASH object: <%=$serializestr%>
result: <%=$out%>
hash binary file
set td [ns_mhash hash new MD5]
set fp [open [ns_url2file /docs/test.bin]]
fconfigure $fp -translation binary -encoding binary
while {![eof $fp]} {
ns_mhash hash update $td [read $fp 1]
}
close $fp
set out [ns_mhash hash end $td]
<%
set td [ns_mhash hash new MD5]
set fp [open [ns_url2file /docs/test.bin]]
fconfigure $fp -translation binary -encoding binary
while {![eof $fp]} {
ns_mhash hash update $td [read $fp 1]
}
close $fp
set out [ns_mhash hash end $td]
%>
result: <%=$out%>
expected result: 4bb051067c1f931b6adf55d1161abfac