Fossil

Check-in [357c3c68]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Enhancements to TH1 related to hashing. Add the 'hash' TH1 command.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | th1Hash
Files: files | file ages | folders
SHA1:357c3c6836355c49fdf6ae154845fbf247368e21
User & Date: mistachkin 2017-03-08 03:22:37
Context
2017-03-08
03:22
Enhancements to TH1 related to hashing. Add the 'hash' TH1 command. Leaf check-in: 357c3c68 user: mistachkin tags: th1Hash
2017-03-07
21:43
Fix the previous fix so that it works even if TH1 hooks are disabled at compile-time. check-in: 691a6492 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/th_main.c.

748
749
750
751
752
753
754

755
756
757
758
759
760
761
...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
...
832
833
834
835
836
837
838





839
840
841
842
843
844
845
846
847
848






















































849
850
851
852
853
854
855
....
1959
1960
1961
1962
1963
1964
1965

1966
1967
1968
1969
1970
1971
1972
** "tclStubs"        = FOSSIL_ENABLE_TCL_STUBS
** "tclPrivateStubs" = FOSSIL_ENABLE_TCL_PRIVATE_STUBS
** "json"            = FOSSIL_ENABLE_JSON
** "markdown"        = FOSSIL_ENABLE_MARKDOWN
** "unicodeCmdLine"  = !BROKEN_MINGW_CMDLINE
** "dynamicBuild"    = FOSSIL_DYNAMIC_BUILD
** "see"             = USE_SEE

**
** Specifying an unknown feature will return a value of false, it will not
** raise a script error.
*/
static int hasfeatureCmd(
  Th_Interp *interp,
  void *p,
................................................................................
  int *argl
){
  int rc = 0;
  const char *zArg;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "hasfeature STRING");
  }
  zArg = (const char *)argv[1];
  if(NULL==zArg){
    /* placeholder for following ifdefs... */
  }
#if defined(FOSSIL_ENABLE_SSL)
  else if( 0 == fossil_strnicmp( zArg, "ssl\0", 4 ) ){
    rc = 1;
  }
................................................................................
    rc = 1;
  }
#endif
#if defined(USE_SEE)
  else if( 0 == fossil_strnicmp( zArg, "see\0", 4 ) ){
    rc = 1;
  }





#endif
  else if( 0 == fossil_strnicmp( zArg, "markdown\0", 9 ) ){
    rc = 1;
  }
  if( g.thTrace ){
    Th_Trace("[hasfeature %#h] => %d<br />\n", argl[1], zArg, rc);
  }
  Th_SetResultInt(interp, rc);
  return TH_OK;
}
























































/*
** TH1 command: tclReady
**
** Return true if the fossil binary has the Tcl integration feature
** enabled and it is currently available for use by TH1 scripts.
................................................................................
    {"encode64",      encode64Cmd,          0},
    {"getParameter",  getParameterCmd,      0},
    {"glob_match",    globMatchCmd,         0},
    {"globalState",   globalStateCmd,       0},
    {"httpize",       httpizeCmd,           0},
    {"hascap",        hascapCmd,            (void*)&zeroInt},
    {"hasfeature",    hasfeatureCmd,        0},

    {"html",          putsCmd,              (void*)&aFlags[0]},
    {"htmlize",       htmlizeCmd,           0},
    {"http",          httpCmd,              0},
    {"insertCsrf",    insertCsrfCmd,        0},
    {"linecount",     linecntCmd,           0},
    {"markdown",      markdownCmd,          0},
    {"puts",          putsCmd,              (void*)&aFlags[1]},







>







 







|







 







>
>
>
>
>










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>







748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
...
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
...
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
....
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
** "tclStubs"        = FOSSIL_ENABLE_TCL_STUBS
** "tclPrivateStubs" = FOSSIL_ENABLE_TCL_PRIVATE_STUBS
** "json"            = FOSSIL_ENABLE_JSON
** "markdown"        = FOSSIL_ENABLE_MARKDOWN
** "unicodeCmdLine"  = !BROKEN_MINGW_CMDLINE
** "dynamicBuild"    = FOSSIL_DYNAMIC_BUILD
** "see"             = USE_SEE
** "hardenedSha1"    = FOSSIL_HARDENED_SHA1
**
** Specifying an unknown feature will return a value of false, it will not
** raise a script error.
*/
static int hasfeatureCmd(
  Th_Interp *interp,
  void *p,
................................................................................
  int *argl
){
  int rc = 0;
  const char *zArg;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "hasfeature STRING");
  }
  zArg = argv[1];
  if(NULL==zArg){
    /* placeholder for following ifdefs... */
  }
#if defined(FOSSIL_ENABLE_SSL)
  else if( 0 == fossil_strnicmp( zArg, "ssl\0", 4 ) ){
    rc = 1;
  }
................................................................................
    rc = 1;
  }
#endif
#if defined(USE_SEE)
  else if( 0 == fossil_strnicmp( zArg, "see\0", 4 ) ){
    rc = 1;
  }
#endif
#if FOSSIL_HARDENED_SHA1
  else if( 0 == fossil_strnicmp( zArg, "hardenedSha1\0", 13 ) ){
    rc = 1;
  }
#endif
  else if( 0 == fossil_strnicmp( zArg, "markdown\0", 9 ) ){
    rc = 1;
  }
  if( g.thTrace ){
    Th_Trace("[hasfeature %#h] => %d<br />\n", argl[1], zArg, rc);
  }
  Th_SetResultInt(interp, rc);
  return TH_OK;
}

/*
** TH1 command: hash STRING ?ALGORITHM?
**
** Returns the cryptographic hash of the specified string.  Possible values
** for the ALGORITHM argument are:
**
** "md5"
** "sha1"
** "sha3-224"
** "sha3-256"
** "sha3-384"
** "sha3-512"
**
** The default algorithm is "sha3-256".  Specifying an unknown algorithm
** will raise a script error.
*/
static int hashCmd(
  Th_Interp *interp,
  void *p,
  int argc,
  const char **argv,
  int *argl
){
  Blob content;
  Blob cksum;
  const char *zAlgorithm = "sha3-256";
  if( argc<2 || argc>3 ){
    return Th_WrongNumArgs(interp, "hash STRING ?ALGORITHM?");
  }
  blob_init(&content, argv[1], argl[1]);
  blob_zero(&cksum);
  if( argc>=3 ){
    zAlgorithm = argv[2];
  }
  if( 0 == fossil_strnicmp( zAlgorithm, "md5\0", 4 ) ){
    md5sum_blob(&content, &cksum);
  }else if( 0 == fossil_strnicmp( zAlgorithm, "sha1\0", 5 ) ){
    sha1sum_blob(&content, &cksum);
  }else if( 0 == fossil_strnicmp( zAlgorithm, "sha3-224\0", 9 ) ){
    sha3sum_blob(&content, 224, &cksum);
  }else if( 0 == fossil_strnicmp( zAlgorithm, "sha3-256\0", 9 ) ){
    sha3sum_blob(&content, 256, &cksum);
  }else if( 0 == fossil_strnicmp( zAlgorithm, "sha3-384\0", 9 ) ){
    sha3sum_blob(&content, 384, &cksum);
  }else if( 0 == fossil_strnicmp( zAlgorithm, "sha3-512\0", 9 ) ){
    sha3sum_blob(&content, 512, &cksum);
  }else{
    Th_SetResult(interp, "unknown hash algorithm", -1);
    return TH_ERROR;
  }
  Th_SetResult(interp, blob_str(&cksum), -1);
  return TH_OK;
}


/*
** TH1 command: tclReady
**
** Return true if the fossil binary has the Tcl integration feature
** enabled and it is currently available for use by TH1 scripts.
................................................................................
    {"encode64",      encode64Cmd,          0},
    {"getParameter",  getParameterCmd,      0},
    {"glob_match",    globMatchCmd,         0},
    {"globalState",   globalStateCmd,       0},
    {"httpize",       httpizeCmd,           0},
    {"hascap",        hascapCmd,            (void*)&zeroInt},
    {"hasfeature",    hasfeatureCmd,        0},
    {"hash",          hashCmd,              0},
    {"html",          putsCmd,              (void*)&aFlags[0]},
    {"htmlize",       htmlizeCmd,           0},
    {"http",          httpCmd,              0},
    {"insertCsrf",    insertCsrfCmd,        0},
    {"linecount",     linecntCmd,           0},
    {"markdown",      markdownCmd,          0},
    {"puts",          putsCmd,              (void*)&aFlags[1]},

Changes to test/th1.test.

1596
1597
1598
1599
1600
1601
1602
1603
1604
1605





















































1606

run_in_checkout {
  fossil test-th-eval --open-config \
      {string length [unversioned content build-icons/src.gif]}
}

test th1-unversioned-2 {$RESULT eq {4592}}

###############################################################################






















































test_cleanup










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659

run_in_checkout {
  fossil test-th-eval --open-config \
      {string length [unversioned content build-icons/src.gif]}
}

test th1-unversioned-2 {$RESULT eq {4592}}

###############################################################################

fossil test-th-eval {hash foo}
test th1-hash-1 {$RESULT eq \
"76d3bc41c9f588f7fcd0d5bf4718f8f84b1c41b20882703100b9eb9413807c01"}

###############################################################################

fossil test-th-eval {hash foo bad}
test th1-hash-2 {$RESULT eq "TH_ERROR: unknown hash algorithm"}

###############################################################################

fossil test-th-eval {hash foo md5}
test th1-hash-3 {$RESULT eq "acbd18db4cc2f85cedef654fccc4a4d8"}

###############################################################################

fossil test-th-eval {hash foo md5 bad}
test th1-hash-4 {$RESULT eq \
"TH_ERROR: wrong # args: should be \"hash STRING ?ALGORITHM?\""}

###############################################################################

fossil test-th-eval {hash foo sha1}
test th1-hash-5 {$RESULT eq "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"}

###############################################################################

fossil test-th-eval {hash foo sha3-224}
test th1-hash-6 {$RESULT eq \
"f4f6779e153c391bbd29c95e72b0708e39d9166c7cea51d1f10ef58a"}

###############################################################################

fossil test-th-eval {hash foo sha3-256}
test th1-hash-7 {$RESULT eq \
"76d3bc41c9f588f7fcd0d5bf4718f8f84b1c41b20882703100b9eb9413807c01"}

###############################################################################

fossil test-th-eval {hash foo sha3-384}
test th1-hash-8 {$RESULT eq [appendArgs \
665551928d13b7d84ee02734502b018d896a0fb87eed5adb4c87ba91bbd6489410e11b0fbcc06 \
ed7d0ebad559e5d3bb5]}

###############################################################################

fossil test-th-eval {hash foo sha3-512}
test th1-hash-9 {$RESULT eq [appendArgs \
4bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c82cbebc68e3b7 \
0a2a1480b4bb5d437a7cba6ecf9d89f9ff3ccd14cd6146ea7e7]}

###############################################################################

test_cleanup

Changes to win/fossil.rc.

110
111
112
113
114
115
116







117
118
119
120
121
122
123
      VALUE "DynamicBuild", "No\0"
#endif
#if defined(FOSSIL_ENABLE_MINIZ)
      VALUE "MinizVersion", "miniz " MZ_VERSION "\0"
#else
      VALUE "ZlibVersion", "zlib " ZLIB_VERSION "\0"
#endif /* defined(FOSSIL_ENABLE_MINIZ) */







#if defined(BROKEN_MINGW_CMDLINE)
      VALUE "CommandLineIsUnicode", "No\0"
#else
      VALUE "CommandLineIsUnicode", "Yes\0"
#endif /* defined(BROKEN_MINGW_CMDLINE) */
#if defined(FOSSIL_ENABLE_SSL)
      VALUE "SslEnabled", "Yes, " OPENSSL_VERSION_TEXT "\0"







>
>
>
>
>
>
>







110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
      VALUE "DynamicBuild", "No\0"
#endif
#if defined(FOSSIL_ENABLE_MINIZ)
      VALUE "MinizVersion", "miniz " MZ_VERSION "\0"
#else
      VALUE "ZlibVersion", "zlib " ZLIB_VERSION "\0"
#endif /* defined(FOSSIL_ENABLE_MINIZ) */
#if FOSSIL_HARDENED_SHA1
      VALUE "Sha1", "Hardened-SHA1 by Marc Stevens and Dan Shumow\0"
#elif defined(FOSSIL_ENABLE_SSL)
      VALUE "Sha1", "OpenSSL-SHA1\0"
#else
      VALUE "Sha1", "Legacy-SHA1\0"
#endif /* FOSSIL_HARDENED_SHA1 */
#if defined(BROKEN_MINGW_CMDLINE)
      VALUE "CommandLineIsUnicode", "No\0"
#else
      VALUE "CommandLineIsUnicode", "Yes\0"
#endif /* defined(BROKEN_MINGW_CMDLINE) */
#if defined(FOSSIL_ENABLE_SSL)
      VALUE "SslEnabled", "Yes, " OPENSSL_VERSION_TEXT "\0"

Changes to www/th1.md.

144
145
146
147
148
149
150

151
152
153
154
155
156
157
...
343
344
345
346
347
348
349

350
351
352


















353
354
355
356
357
358
359
  *  enable\_output
  *  encode64
  *  getParameter
  *  glob\_match
  *  globalState
  *  hascap
  *  hasfeature

  *  html
  *  htmlize
  *  http
  *  httpize
  *  insertCsrf
  *  linecount
  *  markdown
................................................................................
  1. **tclStubs** -- _Uses Tcl stubs (i.e. linking with stubs library)._
  1. **tclPrivateStubs** -- _Uses Tcl private stubs (i.e. header-only)._
  1. **json** -- _Support for the JSON APIs._
  1. **markdown** -- _Support for Markdown documentation format._
  1. **unicodeCmdLine** -- _The command line arguments are Unicode._
  1. **dynamicBuild** -- _Dynamically linked to libraries._
  1. **see** -- _Uses the SQLite Encryption Extension._


Specifying an unknown feature will return a value of false, it will not
raise a script error.



















<a name="html"></a>TH1 html Command
-----------------------------------

  *  html STRING

Outputs the STRING escaped for HTML.







>







 







>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
...
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
  *  enable\_output
  *  encode64
  *  getParameter
  *  glob\_match
  *  globalState
  *  hascap
  *  hasfeature
  *  hash
  *  html
  *  htmlize
  *  http
  *  httpize
  *  insertCsrf
  *  linecount
  *  markdown
................................................................................
  1. **tclStubs** -- _Uses Tcl stubs (i.e. linking with stubs library)._
  1. **tclPrivateStubs** -- _Uses Tcl private stubs (i.e. header-only)._
  1. **json** -- _Support for the JSON APIs._
  1. **markdown** -- _Support for Markdown documentation format._
  1. **unicodeCmdLine** -- _The command line arguments are Unicode._
  1. **dynamicBuild** -- _Dynamically linked to libraries._
  1. **see** -- _Uses the SQLite Encryption Extension._
  1. **hardenedSha1** -- _Uses the <a href="https://github.com/cr-marcstevens/sha1collisiondetection">Hardened-SHA1</a> implementation._

Specifying an unknown feature will return a value of false, it will not
raise a script error.

<a name="hash"></a>TH1 hash Command
-----------------------------------

  *  hash STRING ?ALGORITHM?

Returns the cryptographic hash of the specified string.  Possible values
for the ALGORITHM argument are:

  1. **md5**
  1. **sha1**
  1. **sha3-224**
  1. **sha3-256**
  1. **sha3-384**
  1. **sha3-512**

The default algorithm is "sha3-256".  Specifying an unknown algorithm
will raise a script error.

<a name="html"></a>TH1 html Command
-----------------------------------

  *  html STRING

Outputs the STRING escaped for HTML.