12-system-resources/03-hmac-sha256.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
a6ff945d71c42d469fb7566273a8222084ee8572- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// I159 / D244-C: HMAC authenticates raw Bytes using a shared key.
// This lesson uses the PUBLIC RFC 4231 case 2 test key, not a secret.
//
// Run:
// bin/protos protos/tutorials/12-system-resources/03-hmac-sha256.protos
//
// Expected output: 32, true, true, true (one value per line).
HMAC: import("std:crypto/hmac_sha256")
// Encoding is explicit. HMAC never implicitly converts Strings.
key: Encoding.UTF8.encode("Jefe")
message: Encoding.UTF8.encode("what do ya want for nothing?")
// digest returns a fresh, open standard Bytes containing 32 octets.
tag: HMAC.digest(key, message)
// Known-answer test: compare every octet with RFC 4231 case 2.
expected: Array(91, 220, 193, 70, 191, 96, 117, 78, 106, 4, 36, 38, 8, 149, 117, 199, 90, 0, 63, 8, 157, 39, 57, 131, 157, 236, 88, 185, 100, 236, 56, 67)
matchesExpected: tag.size() == expected.size()
if(matchesExpected) {
index: 0
for(octet in expected) {
if(tag[index] != octet) {
matchesExpected = false
}
index = index + 1
}
}
// verify recomputes HMAC and compares a COMPLETE 32-octet tag.
accepted: HMAC.verify(key, message, tag)
// Changing the message must invalidate the original tag.
tampered: Encoding.UTF8.encode("what do ya want for something?")
rejectedChange: !HMAC.verify(key, tampered, tag)
// A shorter tag is not accepted, even when its prefix is correct.
shortTag: Encoding.UTF8.encode("")
index: 0
while(index < 16) {
shortTag.add(tag[index])
index = index + 1
}
rejectedTruncation: !HMAC.verify(key, message, shortTag)
verified:
matchesExpected &&
accepted &&
rejectedChange &&
rejectedTruncation
if(!verified) {
Error().signal()
}
// Never print real application keys or authentication tags.
print(tag.size())
print(accepted)
print(rejectedChange)
print(rejectedTruncation)