Showing posts with label compression. Show all posts
Showing posts with label compression. Show all posts

Monday, 25 March 2013

PBKDF2 encryption

Do you know the PBKDF2 encryption?
It's the encryption used on Wi-Fi, Mac and more systems. You may read something here: http://en.wikipedia.org/wiki/PBKDF2


refaktor wrote a script to use PBKDF2 encryption, and so you can use it with Rebol!

The source code is here: https://github.com/refaktor/Rebol2-PBKDF2/blob/master/pbkdf2.r

REBOL []
pbkdf2: context [
unsigned-to-binary: func [n [number!] /rev][
  if n > (2 ** 31 - 1) [n: n - (2 ** 32)]
  n: load join "#{" [form to-hex to-integer n "}"]
  either rev [head reverse n][n]
]
calc-sha1: func [pwd salt count key-len /string
  /local hash-len block-len output i j ] [
  hash-len: length? to-string checksum/secure ""
  block-cnt: round/ceiling (key-len / hash-len)
  output: copy #{}
  repeat i block-cnt [
  last: join salt unsigned-to-binary i
  last: xorsum: checksum/key last pwd
  repeat j (count - 1) [
    xorsum: (xorsum xor (last: checksum/key last pwd))
  ]
  output: join output xorsum
  ]
  output: copy/part output key-len
  either string [trim/with enbase/base output 16 #"^/" ] [output ]
]
]

Friday, 22 March 2013

MD5 checksum

If you use Rebol 2, don't use the checksum/method 'md5 since is broken.
Fortunately Andreas Bolka made a script to correct this behavior:

md5sum: func [
    "Returns an MD5 checksum for the contents of the file given."
    fname [file!] /local fport sport chunk-size
] [
    chunk-size: 4096 ;; 4K chunks, just like in md5sum (matches page size)
    fport: open/seek/binary/read fname
    sport: open [scheme: 'checksum algorithm: 'md5]
    while [not tail? fport] [
        insert sport copy/part fport chunk-size
        fport: skip fport chunk-size
    ]
    close fport
    update sport
    sum: copy sport
    close sport
    sum
]


The Rebol 3 checksum works well, unfortunately is not so friendly as the Rebol 2 version: the R3 wants a binary as input, so you have to write:
checksum/method (to-binary read %myfile)   'md5

Thursday, 29 November 2012

Compression

You know that Rebol compress function compress data, but you can manipulate also TAR.GZ archives and ZIP archives.
For TAR.GZ archives there are the following script:
They can be used this way:
do %tar.r
do %gzip.r
write/binary %test.tgz gzip tar [%some-files ...]


Resulting archive is usually smaller than a *.zip of the same files
To decompress use  gunzip.r script

For ZIP archives there is the following script: http://www.rebol.org/download-a-script.r?script-name=rebzip.r
usage: zip/deep %new-zip.zip [ %readme.txt "An example" ftp://192.168.1.10/my-file.txt %my-directory/ ]
to unzip unzip %new-zip.zip

If you want to only explore a zip file without decompress it, you can use this script: http://www.rebol.org/download-a-script.r?script-name=zip.r