7.0 Issues
Peter M. Brigham
pmbrig at gmail.com
Sat Mar 29 11:01:32 EDT 2014
On Mar 28, 2014, at 7:51 PM, Bob Sneidar wrote:
> Not sure about the inserting of pages, but you can store and “reassemble” a pdf by opening it as read binary, reading it into a variable, then saving the variable either as a property or stored in a database, or even encrypted in a file on the HD. (Not sure what the app would be for that…) I’ve often thought of storing my PDF forms in a database this way instead of on the HD.
Richard Gaskin's fwPack/fwUnPack functions make this easy.
-- Peter
Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig
-------------
function fwPack pData, pPassword, pEncryptionMethod
-- fwPack/fwUnPack
-- based on routines by Richard Gaskin, Fourth World Software
-- Generic functions for packing data for Internet transmission or storage,
-- with an optional modest level of password-protected security.
--
-- In the simplest form, fwPack() compresses data with gzip and
-- then encods it with Base64 for robust storage or transmission.
--
-- Data can be optionally protected by supplying a password and
-- an encryption method to the fwPack() function,
-- and subsequently the same password to the fwUnPack() function
-- pEncryptionMethod = "00" -- no encryption, just compress it
-- pEncryptionMethod = "01" uses MD5digest -- fast but only moderately secure
-- pEncryptionMethod = "02" uses SHA1digest -- more secure. However:
-- DO NOT USE THIS ALGORITHM IF YOU REQUIRE INDUSTRIAL-STRENGTH ENCRYPTION.
-- if no password supplied, defaults to no encryption
-- if password supplied but no encryption method specified,
-- defaults to MD5digest if LC version < 4.6
-- otherwise defaults to SHA1digest
-- inverse function is fwUnPack()
-- requires fwUnpack(), _doXor()
-- _doXor() is a private function
if pPassword is empty then
put "00" into pEncryptionMethod
-- no encryption
else if pEncryptionMethod = empty then
-- password supplied but pEncryptionMethod unspecified,
-- default to SHA1digest if possible:
put the version into tVersion
replace "." with empty in tVersion
put "." after char 1 of tVersion
put tVersion >= 4.6 into canUseSHA -- SHA1 only available with LC ≥ 4.6
if canUseSHA then
put "02" into pEncryptionMethod
else
put "01" into pEncryptionMethod
end if
end if
switch pEncryptionMethod
case "00"
-- just compress:
put compress(pData) into dataOut
break
case "01"
-- compress then scramble with MD5-encrypted password:
put compress(pData) into pData
put md5digest(pPassword) into tKeyString
put _doXor(pData,tKeyString) into dataOut
break
case "02"
-- compress then scramble with SHA1-encrypted password:
put compress(pData) into pData
put sha1digest(pPassword) into tKeyString
put _doXor(pData,tKeyString) into dataOut
break
end switch
-- mark with encryption method:
put pEncryptionMethod before dataOut
-- Convert to common low ASCII:
return base64encode(dataOut)
end fwPack
function fwUnPack pData, pPassword
-- fwPack/fwUnPack
-- based on routines by Richard Gaskin, Fourth World Software
-- Generic functions for packing data for robust internet transmission or storage,
-- with an optional modest level of password-protected security.
--
-- fwPack() compresses data with gzip, optionally encrypts it with a password,
-- then encodes it with Base64.
--
-- fwUnPack() will unpack (and decrypt as needed) data compressed with fwPack()
-- requires fwPack(), _doXor()
-- _doXor() is a private function
-- Convert from base64 back to binary:
put base64decode(pData) into pData
-- Check and remove password-protection flag:
put char 1 to 2 of pData into tEncryptionMethod
delete char 1 to 2 of pData
--
switch
case tEncryptionMethod = "00" --no encryption
put pData into dataOut
break
case tEncryptionMethod = "01" -- MD5
-- Get MD5 digest:
put md5digest(pPassword) into tKeyString
put _doXor(pData,tKeyString) into dataOut
break
case tEncryptionMethod = "02" -- SHA1
-- get SHA1 digest
try
put sha1digest(pPassword) into tKeyString
catch tErr
answer "You must be running LiveCode version 4.6 or higher!" as sheet
exit to top
end try
put _doXor(pData,tKeyString) into dataOut
break
end switch
-- Attempt to decompress data, throwing an error if not valid:
try
put decompress(dataOut) into clearData
catch errNo
answer "Wrong password" as sheet
exit to top
end try
return clearData
end fwUnPack
private function _doXor pData,tKeyString
-- utility function for fwPack(), fwUnPack()
put len(tKeyString) into tKeyStringLen
put 0 into i
put empty into dataOut
repeat for each char k in pData
add 1 to i
put char (i wrap tKeyStringLen) of tKeyString into tKeyChar
put numtochar(chartonum(k) bitxor chartonum(tKeyChar)) after dataOut
end repeat
return dataOut
end _doXor
More information about the use-livecode
mailing list