3/31/10

Insert String Every N Characters

This PowerShell v2 script inserts a string every N characters. The interval is specified using the regex repetition operator, {min,max}.
$a = 'abcdefghijklmnopqrstuvwxyz'
([regex]::matches($a, '.{1,3}') | %{$_.value}) -join ' '
([regex]::matches($a, '.{1,8}') | %{$_.value}) -join '..'
([regex]::matches($a, '.{1}') | %{$_.value}) -join '-'

#output
abc def ghi jkl mno pqr stu vwx yz
abcdefgh..ijklmnop..qrstuvwx..yz
a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z

3/22/10

PowerShell Download File Script

This script downloads files asynchronously. It uses the .NET WebClient DownloadFileAsync method.
Note that the download file path must exist.
downloadFile 'http://www.foo.com/foo.iso' 'c:\temp\foo.iso'

function downloadFile($uri, $filename)
{
  $webClient = new-object net.webclient
  $webClient.downloadFileAsync($uri, $filename)
}

3/15/10

JavaScript Namespaces

Two variations for implementing JavaScript namespaces.
The abc namespace syntax is the simplest and useful when only public members are being wrapped.
The xyz namespace syntax can be used for public and private members.
var abc =
{
  version: '1.0',
  foo: function()
  {
    return 'abc.foo';
  }
};

var xyz = function()
{
  //private
  var _version = '2.0';
  function _foo()
  {
    return 'xyz.foo';
  }

  //public
  return {
    version: _version,  //access private var
    foo: function()
    {
      return _foo();  //access private fx
    },
    bar: function(text)
    {
      return 'xyz.bar ' + text;
    }
  };
}();  //self invoking fx

Examples
abc.foo();
abc.version;

xyz.foo();
xyz.bar('test');
xyz.version;

//output
abc.foo
1.0

xyz.foo
xyz.bar test
2.0

Object Graphs in Firebug Watch Window

A slightly modified version showing sub namespaces, eg abc.demo.foo()
var abc = {};
abc.demo =
{
  version: '1.1',
  foo: function()
  {
    return 'abc.demo.foo';
  }
};

var xyz = {};
xyz.demo = function()
{
  //private
  var _version = '2.1';
  function _foo()
  {
    return 'xyz.demo.foo';
  }

  //public
  return {
    version: _version,  //access private var
    foo: function()
    {
      return _foo();  //access private fx
    },
    bar: function(text)
    {
      return 'xyz.demo.bar ' + text;
    }
  };
}();  //self invoking fx

If you use libraries with nested namespaces, local references improve performance by minimizing member lookup.
var abcd = abc.demo;
abcd.foo();
abcd.version;

3/13/10

Hash Message Digest Comparisons

Hash    Bits  Bytes
MD5     128   16  (128/8 = 16)
SHA1    160   20
SHA224  224   28
SHA256  256   32
SHA384  384   48
SHA512  512   64

3/9/10

Password Entropy

Entropy Information Theory - Wikipedia
Password Strength - Wikipedia

Entropy Bits Formula
log N / log 2
N is the number of possible symbols in set
#PowerShell
[math]::log($N)/[math]::log(2)
Entropy Table to 256 Bits
Set Count  Bits Per Char  Set Characters
10         3.322          numeric 0-9
26         4.7            case insensitive alpha a-z
36         5.17           case insensitive alphanumeric a-z, 0-9
52         5.7            case sensitive alpha a-z, A-Z
62         5.954          case sensitive alphanumeric a-z, A-Z, 0-9
94         6.555          ascii characters a-z, A-Z, 0-9, symbols
95         6.57           ascii characters a-z, A-Z, 0-9, symbols including space

Len N=10     N=26     N=36     N=52     N=62     N=94     N=95
1   3.322    4.7      5.17     5.7      5.954    6.555    6.57
2   6.644    9.401    10.34    11.401   11.908   13.109   13.14
3   9.966    14.101   15.51    17.101   17.863   19.664   19.71
4   13.288   18.802   20.68    22.802   23.817   26.218   26.279
5   16.61    23.502   25.85    28.502   29.771   32.773   32.849
6   19.932   28.203   31.02    34.203   35.725   39.328   39.419
7   23.253   32.903   36.189   39.903   41.679   45.882   45.989
8   26.575   37.604   41.359   45.604   47.634   52.437   52.559
9   29.897   42.304   46.529   51.304   53.588   58.991   59.129
10  33.219   47.004   51.699   57.004   59.542   65.546   65.699
11  36.541   51.705   56.869   62.705   65.496   72.1     72.268
12  39.863   56.405   62.039   68.405   71.45    78.655   78.838
13  43.185   61.106   67.209   74.106   77.405   85.21    85.408
14  46.507   65.806   72.379   79.806   83.359   91.764   91.978
15  49.829   70.507   77.549   85.507   89.313   98.319   98.548
16  53.151   75.207   82.719   91.207   95.267   104.873  105.118
17  56.473   79.907   87.889   96.907   101.221  111.428  111.688
18  59.795   84.608   93.059   102.608  107.176  117.983  118.257
19  63.117   89.308   98.229   108.308  113.13   124.537  124.827
20  66.439   94.009   103.399  114.009  119.084  131.092  131.397
21  69.76    98.709   108.568  119.709  125.038  137.646  137.967
22  73.082   103.41   113.738  125.41   130.992  144.201  144.537
23  76.404   108.11   118.908  131.11   136.947  150.756  151.107
24  79.726   112.811  124.078  136.811  142.901  157.31   157.677
25  83.048   117.511  129.248  142.511  148.855  163.865  164.246
26  86.37    122.211  134.418  148.211  154.809  170.419  170.816
27  89.692   126.912  139.588  153.912  160.763  176.974  177.386
28  93.014   131.612  144.758  159.612  166.717  183.528  183.956
29  96.336   136.313  149.928  165.313  172.672  190.083  190.526
30  99.658   141.013  155.098  171.013  178.626  196.638  197.096
31  102.98   145.714  160.268  176.714  184.58   203.192  203.666
32  106.302  150.414  165.438  182.414  190.534  209.747  210.235
33  109.624  155.115  170.608  188.115  196.488  216.301  216.805
34  112.946  159.815  175.777  193.815  202.443  222.856  223.375
35  116.267  164.515  180.947  199.515  208.397  229.411  229.945
36  119.589  169.216  186.117  205.216  214.351  235.965  236.515
37  122.911  173.916  191.287  210.916  220.305  242.52   243.085
38  126.233  178.617  196.457  216.617  226.259  249.074  249.655
39  129.555  183.317  201.627  222.317  232.214  255.629  256.224
40  132.877  188.018  206.797  228.018  238.168  262.184
41  136.199  192.718  211.967  233.718  244.122
42  139.521  197.418  217.137  239.418  250.076
43  142.843  202.119  222.307  245.119  256.03
44  146.165  206.819  227.477  250.819
45  149.487  211.52   232.647  256.52
46  152.809  216.22   237.817
47  156.131  220.921  242.986
48  159.453  225.621  248.156
49  162.774  230.322  253.326
50  166.096  235.022  258.496
51  169.418  239.722
52  172.74   244.423
53  176.062  249.123
54  179.384  253.824
55  182.706  258.524
56  186.028
57  189.35
58  192.672
59  195.994
60  199.316
61  202.638
62  205.96
63  209.281
64  212.603
65  215.925
66  219.247
67  222.569
68  225.891
69  229.213
70  232.535
71  235.857
72  239.179
73  242.501
74  245.823
75  249.145
76  252.467
77  255.788
78  259.11

3/8/10

Alpha

95 characters, including space

0123456789
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
 `!@#$%^&*()-=[]\;',./~_+{}|:"<>?