|
| 1 | +import unittest, strutils, sha1, hmac |
| 2 | + |
| 3 | +proc hexToString(s: string): string = |
| 4 | + var pair: string |
| 5 | + for ch in s: |
| 6 | + pair.add(ch) |
| 7 | + if pair.len == 2: |
| 8 | + result &= fromHex[uint8](pair).char |
| 9 | + pair = "" |
| 10 | + |
| 11 | + |
| 12 | +suite "HMAC": |
| 13 | + |
| 14 | + test "SHA1 (from https://tools.ietf.org/html/rfc2202 section 3)": |
| 15 | + var key: string |
| 16 | + var data: string |
| 17 | + var expectedDigest: string |
| 18 | + var rcvdDigest: string |
| 19 | + |
| 20 | + # test case 1 |
| 21 | + key = hexToString("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b") |
| 22 | + # key length of 20 |
| 23 | + data = "Hi There" |
| 24 | + # data of length 8 |
| 25 | + expectedDigest = "b617318655057264e28bc0b6fb378c8ef146be00" |
| 26 | + rcvdDigest = toHex(hmac_sha1(key, data)) |
| 27 | + check rcvdDigest == expectedDigest |
| 28 | + |
| 29 | + # test case 2 |
| 30 | + key = "Jefe" |
| 31 | + # key length of 4 |
| 32 | + data = "what do ya want for nothing?" |
| 33 | + # data of length 28 |
| 34 | + expectedDigest = "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79" |
| 35 | + rcvdDigest = toHex(hmac_sha1(key, data)) |
| 36 | + check rcvdDigest == expectedDigest |
| 37 | + |
| 38 | + # test case 3 |
| 39 | + key = hexToString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") |
| 40 | + # key length of 20 |
| 41 | + data = "dd".hexToString.repeat(50) |
| 42 | + # data of length 50 |
| 43 | + expectedDigest = "125d7342b9ac11cd91a39af48aa17b4f63f175d3" |
| 44 | + rcvdDigest = toHex(hmac_sha1(key, data)) |
| 45 | + check rcvdDigest == expectedDigest |
| 46 | + |
| 47 | + # test case 4 |
| 48 | + key = hexToString("0102030405060708090a0b0c0d0e0f10111213141516171819") |
| 49 | + # key length of 25 |
| 50 | + data = "cd".hexToString.repeat(50) |
| 51 | + # data of length 50 |
| 52 | + expectedDigest = "4c9007f4026250c6bc8414f9bf50c86c2d7235da" |
| 53 | + rcvdDigest = toHex(hmac_sha1(key, data)) |
| 54 | + check rcvdDigest == expectedDigest |
| 55 | + |
| 56 | + # test case 5 |
| 57 | + key = hexToString("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c") |
| 58 | + # key length of 20 |
| 59 | + data = "Test With Truncation" |
| 60 | + # data of length 20 |
| 61 | + expectedDigest = "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04" |
| 62 | + rcvdDigest = toHex(hmac_sha1(key, data)) |
| 63 | + check rcvdDigest == expectedDigest |
| 64 | + |
| 65 | + # test case 6 |
| 66 | + key = "aa".hexToString.repeat(80) |
| 67 | + # key length of 80 |
| 68 | + data = "Test Using Larger Than Block-Size Key - Hash Key First" |
| 69 | + # data of length 54 |
| 70 | + expectedDigest = "aa4ae5e15272d00e95705637ce8a3b55ed402112" |
| 71 | + rcvdDigest = toHex(hmac_sha1(key, data)) |
| 72 | + check rcvdDigest == expectedDigest |
| 73 | + |
| 74 | + # test case 7 |
| 75 | + key = "aa".hexToString.repeat(80) |
| 76 | + # key length of 80 |
| 77 | + data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" |
| 78 | + # data of length 73 |
| 79 | + expectedDigest = "e8e99d0f45237d786d6bbaa7965c7808bbff1a91" |
| 80 | + rcvdDigest = toHex(hmac_sha1(key, data)) |
| 81 | + check rcvdDigest == expectedDigest |
| 82 | + |
| 83 | + test "SHA256": |
| 84 | + let longKey = "oiJkCotyEAcqEtbHAxwR0sj7Fl4CAT2xdT2oYJep6Wzes2umipBUzocVSwp7nL5ns4xDrPIBEBHKwIr3LlQLZmCw1wStOMSke9SDvQ2Gayj5ZGzvZ1T1uVyN4DcGenYd" |
| 85 | + var rcvdDigest: string |
| 86 | + |
| 87 | + rcvdDigest = toHex(hmac_sha256("ubuntu", "Canonical to offer 5 years of support, but Snap packages mean latest features factor in.")) |
| 88 | + check rcvdDigest == "f53abed8001d0b7c8a64edc011854bded49e1ed55e5d5f5455b7b2ecf6506884" |
| 89 | + |
| 90 | + rcvdDigest = toHex(hmac_sha256(longKey, "Nim (formerly known as \"Nimrod\") is a statically typed, imperative programming language that tries to give the programmer ultimate power without compromises on runtime efficiency. This means it focuses on compile-time mechanisms in all their various forms.")) |
| 91 | + check rcvdDigest == "8df227ae87aee5cad77c395eb4a589469421f4d23ced1a8e93270cd4c4fd9cbf" |
| 92 | + |
| 93 | + |
| 94 | + test "SHA256": |
| 95 | + let longKey = "oiJkCotyEAcqEtbHAxwR0sj7Fl4CAT2xdT2oYJep6Wzes2umipBUzocVSwp7nL5ns4xDrPIBEBHKwIr3LlQLZmCw1wStOMSke9SDvQ2Gayj5ZGzvZ1T1uVyN4DcGenYd" |
| 96 | + var rcvdDigest: string |
| 97 | + |
| 98 | + rcvdDigest = toHex(hmac_sha512(longKey, "In cryptography, a keyed-hash message authentication code (HMAC) is a specific type of message authentication code (MAC) involving a cryptographic hash function and a secret cryptographic key. It may be used to simultaneously verify both the data integrity and the authentication of a message, as with any MAC. Any cryptographic hash function, such as MD5 or SHA-1, may be used in the calculation of an HMAC")) |
| 99 | + check rcvdDigest == "028f744134acb0917e750632133d37dd1da6260be730721a7e6ec44784cd08da653cfb484f4d03805048fe1ae9d881167d8198dfaae5a363358fd39283f9afb7" |
| 100 | + |
| 101 | + test "MD5": |
| 102 | + let longKey = "oiJkCotyEAcqEtbHAxwR0sj7Fl4CAT2xdT2oYJep6Wzes2umipBUzocVSwp7nL5ns4xDrPIBEBHKwIr3LlQLZmCw1wStOMSke9SDvQ2Gayj5ZGzvZ1T1uVyN4DcGenYd" |
| 103 | + var rcvdDigest: string |
| 104 | + |
| 105 | + rcvdDigest = toHex(hmac_md5("Jefe", "what do ya want for nothing?")) |
| 106 | + check rcvdDigest == "750c783e6ab0b503eaa86e310a5db738" |
| 107 | + |
| 108 | + rcvdDigest = toHex(hmac_md5(longKey, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ut nibh sit amet felis volutpat pellentesque eu at tellus. Etiam posuere justo eget mi porta porta.")) |
| 109 | + check rcvdDigest == "35acf8ac84d15ed02a4cd94331fc0aaa" |
0 commit comments