Bug#520265: Missing architecture independent binary representation
Package: ocaml-sha
Version: 1.4-5
Severity: wishlist
Tags: patch
Hi,
I wanted to use ocaml-sha in a situation where I need to store and
load checksums but don't have a in/out_channel. So I've added a
to_binary function to sha1, sha256 and sha512. The resulting string
should be architecture independent just like to_hex but half the size.
Patch attached.
As a sidenote I wanted to use GPLv3 for my project but ocaml-sha is
licensed under GPLv2 only. Have you ever asked the author to change
the license to maybe LGPLv2+ or GPLv2+?
MfG
Goswin
-- System Information:
Debian Release: squeeze/sid
APT prefers unstable-i386
APT policy: (500, 'unstable-i386'), (500, 'experimental-i386'), (500, 'unstable'), (200, 'experimental')
Architecture: amd64 (x86_64)
Kernel: Linux 2.6.22.2-mrvn
Locale: LANG=C, LC_CTYPE=de_DE (charmap=ISO-8859-1)
Shell: /bin/sh linked to /bin/bash
diff -u ocaml-sha-1.4/debian/changelog ocaml-sha-1.4/debian/changelog
--- ocaml-sha-1.4/debian/changelog
+++ ocaml-sha-1.4/debian/changelog
@@ -1,3 +1,10 @@
+ocaml-sha (1.4-5.a0.mrvn.1) unstable; urgency=low
+
+ [ Goswin von Brederlow ]
+ * Add to_binary function
+
+ -- Goswin von Brederlow <goswin-v-b@web.de> Wed, 18 Mar 2009 14:25:03 +0100
+
ocaml-sha (1.4-5) unstable; urgency=low
[ Eric Cooper ]
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha256.ml
+++ ocaml-sha-1.4/sha256.ml
@@ -22,6 +22,7 @@
external finalize: ctx -> t = "stub_sha256_finalize"
external to_hex: t -> string = "stub_sha256_to_hex"
external file_fast: string -> t = "stub_sha256_file"
+external to_binary: t -> string = "stub_sha256_to_binary"
end
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha512.ml
+++ ocaml-sha-1.4/sha512.ml
@@ -22,6 +22,8 @@
external finalize: ctx -> t = "stub_sha512_finalize"
external to_hex: t -> string = "stub_sha512_to_hex"
external file_fast: string -> t = "stub_sha512_file"
+external to_binary: t -> string = "stub_sha512_to_binary"
+
end
include Sha.Sha(Sha512ops)
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha1.ml
+++ ocaml-sha-1.4/sha1.ml
@@ -22,6 +22,7 @@
external finalize: ctx -> t = "stub_sha1_finalize"
external to_hex: t -> string = "stub_sha1_to_hex"
external file_fast: string -> t = "stub_sha1_file"
+external to_binary: t -> string = "stub_sha1_to_binary"
end
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha1.mli
+++ ocaml-sha-1.4/sha1.mli
@@ -45,3 +45,6 @@
(** return a printable hexadecimal representation of the given digest *)
val to_hex : t -> string
+
+(** return a binary representation of the given digest *)
+val to_binary : t -> string
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha1_stubs.c
+++ ocaml-sha-1.4/sha1_stubs.c
@@ -262,6 +262,17 @@
#undef D
}
+/**
+ * sha1_to_binary - Transform the SHA1 digest into a binary data
+ */
+static inline void sha1_to_binary(sha1_digest *digest, sha1_digest *out)
+{
+ int i;
+
+ for(i = 0; i < 5; ++i)
+ out->digest[i] = cpu_to_be32(digest->digest[i]);
+}
+
#include <unistd.h>
#include <fcntl.h>
@@ -348,3 +359,15 @@
CAMLreturn(result);
}
+
+CAMLprim value stub_sha1_to_binary(value digest)
+{
+ CAMLparam1(digest);
+ CAMLlocal1(result);
+
+ result = caml_alloc_string(20);
+ sha1_to_binary((sha1_digest *)digest,
+ (sha1_digest *)String_val(result));
+
+ CAMLreturn(result);
+}
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha.ml
+++ ocaml-sha-1.4/sha.ml
@@ -21,6 +21,7 @@
val finalize: ctx -> t
val to_hex: t -> string
val file_fast: string -> t
+ val to_binary: t -> string
end
module Sha = functor(Shaops: SHAOPS) -> struct
@@ -75,4 +76,6 @@
let file_fast file = Shaops.file_fast file
+let to_binary digest = Shaops.to_binary digest
+
end
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha512.mli
+++ ocaml-sha-1.4/sha512.mli
@@ -45,3 +45,6 @@
(** return a printable hexadecimal representation of the given digest *)
val to_hex : t -> string
+
+(** return a binary representation of the given digest *)
+val to_binary : t -> string
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha256_stubs.c
+++ ocaml-sha-1.4/sha256_stubs.c
@@ -208,6 +208,17 @@
snprintf(p, 9, "%08x", be32_to_cpu(digest->digest[i]));
}
+/**
+ * sha256_to_binary - Transform the SHA256 digest into a binary data
+ */
+static inline void sha256_to_binary(sha256_digest *digest, sha256_digest *out)
+{
+ int i;
+
+ for(i = 0; i < 8; ++i)
+ out->digest[i] = cpu_to_be32(digest->digest[i]);
+}
+
#include <unistd.h>
#include <fcntl.h>
@@ -293,3 +304,15 @@
CAMLreturn(result);
}
+
+CAMLprim value stub_sha256_to_binary(value digest)
+{
+ CAMLparam1(digest);
+ CAMLlocal1(result);
+
+ result = caml_alloc_string(32);
+ sha256_to_binary((sha256_digest *)digest,
+ (sha256_digest *)String_val(result));
+
+ CAMLreturn(result);
+}
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha256.mli
+++ ocaml-sha-1.4/sha256.mli
@@ -45,3 +45,6 @@
(** return a printable hexadecimal representation of the given digest *)
val to_hex : t -> string
+
+(** return a binary representation of the given digest *)
+val to_binary : t -> string
only in patch2:
unchanged:
--- ocaml-sha-1.4.orig/sha512_stubs.c
+++ ocaml-sha-1.4/sha512_stubs.c
@@ -229,6 +229,17 @@
snprintf(p, 17, "%016llx", be64_to_cpu(digest->digest[i]));
}
+/**
+ * sha512_to_binary - Transform the SHA512 digest into a binary data
+ */
+static inline void sha512_to_binary(sha512_digest *digest, sha512_digest *out)
+{
+ int i;
+
+ for(i = 0; i < 8; ++i)
+ out->digest[i] = cpu_to_be64(digest->digest[i]);
+}
+
#include <unistd.h>
#include <fcntl.h>
@@ -314,3 +325,15 @@
CAMLreturn(result);
}
+
+CAMLprim value stub_sha512_to_binary(value digest)
+{
+ CAMLparam1(digest);
+ CAMLlocal1(result);
+
+ result = caml_alloc_string(64);
+ sha512_to_binary((sha512_digest *)digest,
+ (sha512_digest *)String_val(result));
+
+ CAMLreturn(result);
+}
Reply to: