瀏覽代碼

Make canonical (sorted) strings from data

htlogr::data will now sort data by keys before assembling it all into
message so that order will not differ between invocations.

Added to both Perl and Python implementations.
Alois Mahdal 11 年之前
父節點
當前提交
e997867b14
共有 2 個檔案被更改,包括 3 行新增2 行删除
  1. 1
    1
      lib/htlogr.pm
  2. 2
    1
      lib/htlogr.py

+ 1
- 1
lib/htlogr.pm 查看文件

67
     carp("data must be a hash reference") unless ref $data == 'HASH';
67
     carp("data must be a hash reference") unless ref $data == 'HASH';
68
 
68
 
69
     my @fields;
69
     my @fields;
70
-    foreach (keys %$data) {
70
+    foreach (sort keys %$data) {
71
         push @fields, join $DIV_VALUE, $_, $data->$_;
71
         push @fields, join $DIV_VALUE, $_, $data->$_;
72
     }
72
     }
73
 
73
 

+ 2
- 1
lib/htlogr.py 查看文件

40
 
40
 
41
     def _serialize(self, data):
41
     def _serialize(self, data):
42
         fields = []
42
         fields = []
43
-        for key, value in data.iteritems():
43
+        for key in sorted(data.keys()):
44
+            value = data[key]
44
             fields.append("%s%s%s" % (key, self.DIV_VALUE, value))
45
             fields.append("%s%s%s" % (key, self.DIV_VALUE, value))
45
         return self.DIV_FIELD.join(fields)
46
         return self.DIV_FIELD.join(fields)
46
 
47