Digest Object provides applications the functionality of a message digest algorithm, such as MD5, SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.
A Digest object starts out initialized. The data is processed through it using the update methods. At any point reset can be called to reset the digest.
Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash computation.The digest method can be called once for a given number of updates. After digest has been called, the Digest object is reset to its initialized state.
1 MD5
The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities. It can still be used as a checksum to verify data integrity, but only against unintentional corruption. It remains suitable for other non-cryptographic purposes, for example for determining the partition for a particular key in a partitioned database.
<script> var digest = new Ax.crypt.Digest("MD5"); var hello1 = "Hello World 1"; var hello2 = "Hello World 2"; var digest1 = digest.update(hello1).digest(); var digest2 = digest.update(hello2).digest(); // CASE 1 console.log("1:" + digest1); console.log("1:" + digest.update(hello1).digest()); Ax.jsunit.assertEquals(digest1, digest.update(hello1).digest()); // CASE 2 console.log("2:" + digest2); console.log("2:" + digest.update(hello2).digest()); Ax.jsunit.assertEquals(digest2, digest.update(hello2).digest()); // CASE 3 var digest3a = digest.update(hello1) .update(hello2) .digest(); var digest3b = digest.update(hello1) .update(hello2) .digest(); console.log("3:" + digest3a); console.log("3:" + digest3b); Ax.jsunit.assertEquals(digest3a, digest3b); // CASE 4 var digest4 = digest.update(hello1) .reset() .update(hello2) .digest(); console.log("4:" + digest4); Ax.jsunit.assertEquals(digest2, digest4); // CASE 5 console.log("5:" + digest.update(new Ax.net.URL("https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv")).digest()); </script>
1:3fe10bf44e9a7deab63ea946c04fbcd8
1:3fe10bf44e9a7deab63ea946c04fbcd8
2:4c24aac86aa49adce486631bf365098f
2:4c24aac86aa49adce486631bf365098f
3:6c1b6727930c1dd282dcad1b5d47e1bb
3:6c1b6727930c1dd282dcad1b5d47e1bb
4:4c24aac86aa49adce486631bf365098f
5:32078264d936c895907f1de187734274
2 SHA
The Secure Hash Algorithms are a family of cryptographic hash functions published by the National Institute of Standards and Technology (NIST) as a U.S. Federal Information Processing Standard (FIPS), including:
- SHA-0: A retronym applied to the original version of the 160-bit hash function published in 1993 under the name "SHA". It was withdrawn shortly after publication due to an undisclosed "significant flaw" and replaced by the slightly revised version SHA-1.
- SHA-1: A 160-bit hash function which resembles the earlier MD5 algorithm. This was designed by the National Security Agency (NSA) to be part of the Digital Signature Algorithm. Cryptographic weaknesses were discovered in SHA-1, and the standard was no longer approved for most cryptographic uses after 2010.
- SHA-2: A family of two similar hash functions, with different block sizes, known as SHA-256 and SHA-512. They differ in the word size; SHA-256 uses 32-bit words where SHA-512 uses 64-bit words. There are also truncated versions of each standard, known as SHA-224, SHA-384, SHA-512/224 and SHA-512/256. These were also designed by the NSA.
- SHA-3: A hash function formerly called Keccak, chosen in 2012 after a public competition among non-NSA designers. It supports the same hash lengths as SHA-2, and its internal structure differs significantly from the rest of the SHA family.
<script> var payload = "This is a text message"; var digest1 = new Ax.crypt.Digest("SHA-1"); console.log(digest1.update(payload).digest()); var digest256 = new Ax.crypt.Digest("SHA-256"); console.log(digest256.update(payload).digest()); var digest512 = new Ax.crypt.Digest("SHA-512"); console.log(digest512.update(payload).digest()); </script>
e266e7e8414925efc8cfce82e21024ad616b58bd
36aef235b8738c9162eb4a118d504ecf0ca9d5a8ac943d942adf389fc589d9f5
643cdac522adaa3821ef6bba27065cf76cf396cfb72f6066e22440bdac556b27f4c451e52c8d71462bae9902ab3d8fc49b4b2841fe6883ec2e24ca3188267c9a
3 ResultSet digest
Digest will normally accept as input any object that can be converted to a byte[] such as a file, blob, string, etc. But it also accepts a ResultSet.
Hashing a ResultSet may be useful for junit tests so we can compute a hash on input data and assert this data reamins constant (unchanged) between succesive tests.
<script> var rs = Ax.db.executeQuery("SELECT * FROM systables ORDER by tabid"); var digest1 = new Ax.crypt.Digest("MD5"); console.log(digest1.update(rs).digest()); </script>
0f5bb262c08df0dc596c8d77f60f804b
Notice that ResultSet may provide same columns and rows in same order to compute the same hash. So don't forget to use an specific order by.