From aa939ecf97fb90771bd153a55fab9b765e8bfa34 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Tue, 21 May 2024 15:52:00 -0600 Subject: [PATCH 1/9] Create hexlib.ml Adds two functions. 1. decode_hex_string to convert a hexidecimal string to an ascii string. 2. encode_to_hex to convert an ascii string to hexidecimal. Both functions leverage the Hex library. --- data/cookbook/encode-decode-hex/hexlib.ml | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 data/cookbook/encode-decode-hex/hexlib.ml diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml new file mode 100644 index 0000000000..fcc89bb160 --- /dev/null +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -0,0 +1,32 @@ +--- +packages: [Hex, Cstruct] +discussion: | + - **Using the Hex and Cstruct libraries to decode a hexidecimal string to an ascii string. + - **Using the Hex library to convert an ascii string to a hexidecimal encoded message. + - **Leverages the Hex and Cstruct libraries. +--- + +(* Decode a hex string *) +let decode_hex_string (hex_string : string) : string = + let byte_string = Hex.to_cstruct (`Hex hex_string) in + let decoded_string = Cstruct.to_string byte_string in + decoded_string +;; + +(* Encode a string to hex *) +let encode_to_hex (message : string) : string = + Hex.of_string message +;; + +(* Example *) +let secret_message = "48656c6c6f2c20576f726c6421" +let decrypted_message = decode_hex_string secret_message;; + +(* Show the decrypted message *) +print_endline decrypted_message + +(* Encrypt the message back to hexidecimal *) +let encoded_message = encode_to_hex secret_message;; + +(* Show the hexidecimal encoded message *) +Hex.show encoded_message From d809e1daabd39dacadc00e4e53c2bf8262177bfe Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Wed, 22 May 2024 19:16:30 -0600 Subject: [PATCH 2/9] Update hexlib.ml Added doc string for each function and updated encoder to use Cstruct like in decoder. Formatted example printing. --- data/cookbook/encode-decode-hex/hexlib.ml | 26 ++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml index fcc89bb160..186e13ed8b 100644 --- a/data/cookbook/encode-decode-hex/hexlib.ml +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -3,30 +3,36 @@ packages: [Hex, Cstruct] discussion: | - **Using the Hex and Cstruct libraries to decode a hexidecimal string to an ascii string. - **Using the Hex library to convert an ascii string to a hexidecimal encoded message. - - **Leverages the Hex and Cstruct libraries. + - **Uses the Hex and Cstruct libraries. --- -(* Decode a hex string *) -let decode_hex_string (hex_string : string) : string = + +(* [decode_hex_string hex_string] is the string represented by [hex_string]. + Raises: Invalid_argument if [hex_string] is not valid hex. *) +let decode_hex_string hex_string = let byte_string = Hex.to_cstruct (`Hex hex_string) in let decoded_string = Cstruct.to_string byte_string in decoded_string ;; -(* Encode a string to hex *) -let encode_to_hex (message : string) : string = - Hex.of_string message +(** [encode_to_hex message] is the hex string representation of [message]. *) +let encode_to_hex message = + let byte_string = Cstruct.of_string message in + let hex_string = Hex.of_cstruct byte_string in + Hex.to_string hex_string ;; -(* Example *) -let secret_message = "48656c6c6f2c20576f726c6421" +(* Example usage *) +let secret_message = "48656c6c6f2c20576f726c6421";; +Printf.printf "Secret message: %s\n" secret_message + let decrypted_message = decode_hex_string secret_message;; (* Show the decrypted message *) -print_endline decrypted_message +Printf.printf "Decoded message: %s\n" decrypted_message (* Encrypt the message back to hexidecimal *) let encoded_message = encode_to_hex secret_message;; (* Show the hexidecimal encoded message *) -Hex.show encoded_message +Printf.printf "Encoded message: %s\n" encoded_message;; From 2a3c0d62c7b3dcee1e9ea0a2c70c4583c3ef113b Mon Sep 17 00:00:00 2001 From: Christine Rose Date: Fri, 24 May 2024 12:34:50 +0100 Subject: [PATCH 3/9] Minor formatting --- data/cookbook/encode-decode-hex/hexlib.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml index 186e13ed8b..8da445d10a 100644 --- a/data/cookbook/encode-decode-hex/hexlib.ml +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -1,8 +1,8 @@ --- packages: [Hex, Cstruct] discussion: | - - **Using the Hex and Cstruct libraries to decode a hexidecimal string to an ascii string. - - **Using the Hex library to convert an ascii string to a hexidecimal encoded message. + - **Using the Hex and Cstruct libraries to decode a hexidecimal string to an ASCII string. + - **Using the Hex library to convert an ASCII string to a hexidecimal encoded message. - **Uses the Hex and Cstruct libraries. --- From e0976cef07691f6b13a6949795b606980e385214 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Tue, 11 Jun 2024 18:55:08 -0600 Subject: [PATCH 4/9] Fix packages format Fixed the format for the packages section. Now it lists each package individually, the tested_version, and the used_libraries. --- data/cookbook/encode-decode-hex/hexlib.ml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml index 8da445d10a..1534fa5ff6 100644 --- a/data/cookbook/encode-decode-hex/hexlib.ml +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -1,5 +1,13 @@ --- -packages: [Hex, Cstruct] +packages: +- name: "hex" + tested_version: "1.5.0" + used_libraries: + - hex +- name: "cstruct" + tested_version: "6.2.0" + used_libraries: + - cstruct discussion: | - **Using the Hex and Cstruct libraries to decode a hexidecimal string to an ASCII string. - **Using the Hex library to convert an ASCII string to a hexidecimal encoded message. From 33a351565e1eb2cd4bab51b8bc1f9e8cd1166668 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:08:42 -0600 Subject: [PATCH 5/9] Update hexlib.ml updated code comments to remove odoc/ocamlfmt style comments --- data/cookbook/encode-decode-hex/hexlib.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml index 1534fa5ff6..a0ffae9d64 100644 --- a/data/cookbook/encode-decode-hex/hexlib.ml +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -15,15 +15,15 @@ discussion: | --- -(* [decode_hex_string hex_string] is the string represented by [hex_string]. - Raises: Invalid_argument if [hex_string] is not valid hex. *) +(* `decode_hex_string hex_string` is the string represented by `hex_string`. + Raises: Invalid_argument if `hex_string` is not valid hex. *) let decode_hex_string hex_string = let byte_string = Hex.to_cstruct (`Hex hex_string) in let decoded_string = Cstruct.to_string byte_string in decoded_string ;; -(** [encode_to_hex message] is the hex string representation of [message]. *) +(* `encode_to_hex message` is the hex string representation of `message`. *) let encode_to_hex message = let byte_string = Cstruct.of_string message in let hex_string = Hex.of_cstruct byte_string in From c2b39f18398f93dc8da9ac7a60c152ef551cdac3 Mon Sep 17 00:00:00 2001 From: Cuihtlauac Alvarado Date: Tue, 18 Jun 2024 14:31:38 +0200 Subject: [PATCH 6/9] Apply suggestions from code review --- data/cookbook/encode-decode-hex/hexlib.ml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml index a0ffae9d64..6eca60fac4 100644 --- a/data/cookbook/encode-decode-hex/hexlib.ml +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -21,14 +21,12 @@ let decode_hex_string hex_string = let byte_string = Hex.to_cstruct (`Hex hex_string) in let decoded_string = Cstruct.to_string byte_string in decoded_string -;; (* `encode_to_hex message` is the hex string representation of `message`. *) let encode_to_hex message = let byte_string = Cstruct.of_string message in let hex_string = Hex.of_cstruct byte_string in Hex.to_string hex_string -;; (* Example usage *) let secret_message = "48656c6c6f2c20576f726c6421";; @@ -42,5 +40,5 @@ Printf.printf "Decoded message: %s\n" decrypted_message (* Encrypt the message back to hexidecimal *) let encoded_message = encode_to_hex secret_message;; -(* Show the hexidecimal encoded message *) +(* Show the hexadecimal encoded message *) Printf.printf "Encoded message: %s\n" encoded_message;; From 2590a5140894e26db3ea0d2cae59a121bc7e0202 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Tue, 18 Jun 2024 10:22:31 -0600 Subject: [PATCH 7/9] Update hexlib.ml - Removed secret in variable name - fixed error in encode function by changing Hex.to_string to Hex.show - added some more documentation to the encode function --- data/cookbook/encode-decode-hex/hexlib.ml | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml index 6eca60fac4..15d051ca35 100644 --- a/data/cookbook/encode-decode-hex/hexlib.ml +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -16,29 +16,30 @@ discussion: | (* `decode_hex_string hex_string` is the string represented by `hex_string`. - Raises: Invalid_argument if `hex_string` is not valid hex. *) + Raises: Invalid_argument if `hex_string` is not valid hex. *) let decode_hex_string hex_string = let byte_string = Hex.to_cstruct (`Hex hex_string) in let decoded_string = Cstruct.to_string byte_string in decoded_string -(* `encode_to_hex message` is the hex string representation of `message`. *) +(* `encode_to_hex message` accepts a string `message` and returns + the hexidecimal representation of `message`. *) let encode_to_hex message = let byte_string = Cstruct.of_string message in let hex_string = Hex.of_cstruct byte_string in - Hex.to_string hex_string + Hex.show hex_string (* Example usage *) -let secret_message = "48656c6c6f2c20576f726c6421";; -Printf.printf "Secret message: %s\n" secret_message +let hex_message = "48656c6c6f2c20576f726c6421";; +Printf.printf "Hex message: %s\n" hex_message -let decrypted_message = decode_hex_string secret_message;; +let decrypted_message = decode_hex_string hex_message;; (* Show the decrypted message *) Printf.printf "Decoded message: %s\n" decrypted_message (* Encrypt the message back to hexidecimal *) -let encoded_message = encode_to_hex secret_message;; +let encoded_message = encode_to_hex decrypted_message;; (* Show the hexadecimal encoded message *) -Printf.printf "Encoded message: %s\n" encoded_message;; +Printf.printf "Encoded message: %s\n" encoded_message From bc1f05d7ec9a864651c59d2de81f4babd56b62b6 Mon Sep 17 00:00:00 2001 From: Grant Smith <57376089+ggsmith842@users.noreply.github.com> Date: Tue, 18 Jun 2024 10:30:03 -0600 Subject: [PATCH 8/9] Update hexlib.ml Fix spelling errors --- data/cookbook/encode-decode-hex/hexlib.ml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml index 15d051ca35..65dac624f7 100644 --- a/data/cookbook/encode-decode-hex/hexlib.ml +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -9,8 +9,8 @@ packages: used_libraries: - cstruct discussion: | - - **Using the Hex and Cstruct libraries to decode a hexidecimal string to an ASCII string. - - **Using the Hex library to convert an ASCII string to a hexidecimal encoded message. + - **Using the Hex and Cstruct libraries to decode a hexadecimal string to an ASCII string. + - **Using the Hex library to convert an ASCII string to a hexadecimal encoded message. - **Uses the Hex and Cstruct libraries. --- @@ -23,7 +23,7 @@ let decode_hex_string hex_string = decoded_string (* `encode_to_hex message` accepts a string `message` and returns - the hexidecimal representation of `message`. *) + the hexadecimal representation of `message`. *) let encode_to_hex message = let byte_string = Cstruct.of_string message in let hex_string = Hex.of_cstruct byte_string in @@ -38,7 +38,7 @@ let decrypted_message = decode_hex_string hex_message;; (* Show the decrypted message *) Printf.printf "Decoded message: %s\n" decrypted_message -(* Encrypt the message back to hexidecimal *) +(* Encrypt the message back to hexadecimal *) let encoded_message = encode_to_hex decrypted_message;; (* Show the hexadecimal encoded message *) From e6ebd43e20752f28bcefcaa1b98a4def91d276e3 Mon Sep 17 00:00:00 2001 From: Cuihtlauac Alvarado Date: Wed, 19 Jun 2024 07:58:18 +0200 Subject: [PATCH 9/9] Apply suggestions from code review --- data/cookbook/encode-decode-hex/hexlib.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data/cookbook/encode-decode-hex/hexlib.ml b/data/cookbook/encode-decode-hex/hexlib.ml index 65dac624f7..f68c0e69b6 100644 --- a/data/cookbook/encode-decode-hex/hexlib.ml +++ b/data/cookbook/encode-decode-hex/hexlib.ml @@ -33,13 +33,13 @@ let encode_to_hex message = let hex_message = "48656c6c6f2c20576f726c6421";; Printf.printf "Hex message: %s\n" hex_message -let decrypted_message = decode_hex_string hex_message;; +let message = decode_hex_string hex_message;; (* Show the decrypted message *) -Printf.printf "Decoded message: %s\n" decrypted_message +Printf.printf "Decoded message: %s\n" message (* Encrypt the message back to hexadecimal *) -let encoded_message = encode_to_hex decrypted_message;; +let encoded_message = encode_to_hex message;; (* Show the hexadecimal encoded message *) Printf.printf "Encoded message: %s\n" encoded_message