Base32 decode in actionscript - actionscript

How do I decode a Base32 encoded string in Actionscript?
/Martin

Here's a JavaScript library that does Base32:
http://www.tumuski.com/2010/04/nibbler/
It might just work out the box in ActionScript, since both are ECMAScript.

Base32? Just out of curiosity: Who would use that? I suggest searching for a Base64 library (there are 2 or 3 on the web) and modifying it. Should be quite straight forward.
greetz
back2dos

Related

charachter encoding in PHP Extension

I'm currently writing a PHP extension in C++ with the Zend API. Basically I make PHP_METHOD{..} wrappers around my native C++ interface methods and using "zend_parse_parameters(..)" to fetch the corresponding input arguments.
This extension contains methods which can take strings as arguments, such as a filename.
I know from http://php.net/manual/en/language.types.string.php#language.types.string.details that strings have no encoding in PHP, but still can I expect from the PHP programmer that he will use a function like "utf8_decode(..)" such that the input strings can be read by the extension correctly?
Or does the PHP Programmer expect that the extension detects the encoding from the php-script and handles strings accordingly?
Every help is highly appreciated! Thanks!
You are correct. Strings are just binary blobs in PHP. As the author of an extension. Your options:
Have the user hand your extension UTF-8: By far the best option. The user has to make the decision. Assert that the string is UTF-8 encodable and fail early.
Encode yourself: You cannot know the meaning of the string. As PHP strings are just binary blobs and have no encoding information you do not know what the intended string content is. It might as well just come from a Windows file with weird encoding and was concatenated with a complete different encoding. Worse, it might be UTF-8 encodable, but actually not UTF-8, in which way you interpret it wrongly, without the user knowing. Hence, solution 1, have the user pass UTF-8.
Alternative: Force the user to pass an input encoding.
Here is an example of the alterantive 3:
$obj = MyExtensionClass('UTF-8'); // force encoding
$obj->someMethod($inputStr); // try to convert now
The standard library uses approach 1. See json_encode as an example:

Delphi - get encoding for a given file

I have read this question which I thought would give me what I was after:
How Can I Best Guess the Encoding when the BOM (Byte Order Mark) is Missing?
I would like to know if there is another way to get the file encoding, without using Mozilla's i18n component in D2006? I can not use other 3d party components.
I have read the all the answers from original question, and I can not use the interface provided because the client doesn't accept the deployment of that dll:
first answer - https://stackoverflow.com/a/373103/368364 - nothing conclusive.
second answer - http://www.siao2.com/2007/04/22/2239345.aspx - regarding and reading the comments give you a clue!
third answer - How Can I Best Guess the Encoding when the BOM (Byte Order Mark) is Missing? - user choice the encoding.
Some of the links provided in the original question are dead, and none address my problem, which is:
How to get the file encoding without using 3rd party components?
I would look for a BOM first and if one is not found call IsTextUnicode. But beware that no method is foolproof.
Determining the encoding of a file seems to be problematic. It appears that some of the UTF8 files do not have a BOM. This appears to work:
InputData.LoadFromFile(f,TEncoding.UTF8);
if InputData.count=0 then
InputData.LoadFromFile(f);
Is there a better approach. I know this solution isn't very elegant.

SHA256 implementation using Base64 for input and output

I've been asked to develop the company's backoffice for the iPad and, while developing the login screen, I've ran into an issue with the authentication process.
The passwords are concatenated with a salt, hashed using SHA-256 and stored in the database.
The backoffice is Flash-based and uses the as3crypto library to hash then password+salt and my problem is that the current implementation uses Base64 for both input and output.
This site demonstrates how this can be done: just select Hash and select Base64 for both input and output format and fire away. So far, all my attempts have yielded different results from the ones this site (and the backoffice code) give me.
While I think that in theory it should be relatively simply:
Base64 encode the pass+salt
Hash it using SHA-256
Base64 encode the result again
so far I haven't been able to do this and I'm getting quite the headache to be honest.
My code is becoming a living maze, i'll have to redo-it tomorrow I reckon.
Any ideas?
Cheers and thanks in advance
PS: Here's the Backoffice's Flash code for generating hashed passwords by the way:
var currentResult:ByteArray;
var hash:IHash = Crypto.getHash('sha256');
var data:ByteArray = Base64.decodeToByteArray(str + vatel);
currentResult = hash.hash(data);
return Base64.encodeByteArray(currentResult).toString();
The backoffice code does not do
Base64 encode the pass+salt
Hash it using SHA-256
Base64 encode the result again
(as you wrote above)
Instead, what it does is
Base64 decode the pass+salt string into a byte array
Hash the byte array using SHA-256
Base64 encode the byte array, returning a string
As per step 1 above, it's a unclear what kind of character encoding the input strings uses. You need to make sure that both systems use the same encoding for the input strings! UTF8, UTF16-LE or UTF16-BE makes a world of a difference in this case!
Start by finding out the correct character encoding to use on the iOS side.
Oh, and Matt Gallagher has written an easy to use wrapper class for hashes to use on iOS, HashValue.m, I've used it with good results.

Encrypt/Decrypt String

I have a simple problem for that I'd like to hear your thoughts:
I have this URL in Rails http://example.com/hosts/show/somehost
I'm getting the 'somehost' part via params[:id]. I'm calling URI.encode on 'somehost' but this does not encode '.' characters. Rails won't recognize ID parts with points in it so I tried to replace the points with '%2E' - That works, but Firefox (and I guess other browsers too) changes the '%2E' back to points right after the request. This makes copy&paste impossible and will lead to a lot of problems.
I'd like to encrypt and decrypt the 'somehost' part in an URL-safe way - Any suggestions? I can't call by an numeric primary key because of the underlying architecture. I have to look up by name.
Thank you all very much!
You could use base64 encoding, but it would be better to fix the actual problem you are having. This issue is described here. You need to set a :requirements key for your routes file with a regex that includes the dot.

sha256 encryption in erlang

i have written some code in php there i have use mhash(MHASH_SHA256, $key) and its giving result as expected.i wanna know how we can achieve same thing in erlang.i can see in crypto their is one inbuild sha function is their but i dont think so its mean for sha256.
any suggestion what i have to do ?
thank you in advance.
Have you seen this page, which links to an SHA-256 module for Erlang?
EDIT: Apparently that code is obsolete, replaced by this module. If that still doesn't do what you want (in terms of hex/binary) I suggest you email its author, preferably with a patch.
It seems to me that the return value of the mentioned sha2 module depends on your input. If you call it with a binary, the result is binary; if you call it with a string, the result is a string:
sha2:hexdigest256("Zed").
"a90e4dc685583c72296ca49b5d0bb148f2e1197a805b2a1d2ff6d17b4398b2be"
sha2:hexdigest256(<<"Zed">>).
<<169,14,77,198,133,88,60,114,41,108,164,155,93,11,177,72,
242,225,25,122,128,91,42,29,47,246,209,123,67,...>>

Resources