Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i encode a string in base64 using meteor

I am trying to use a form to upload files to a s3 bucket using Meteor. I am following this amazon article. At "Sign Your S3 POST Form", near the end, I need to encode a string to base64 but I've been unable to find a way to do this. Can anyone tell me how to do this? Notice that the string first needs to be encoded and then signed. This is how it's done in python:

import base64
import hmac, hashlib

policy = base64.b64encode(policy_document)
signature = base64.b64encode(hmac.new(AWS_SECRET_ACCESS_KEY, policy, hashlib.sha1).digest())

1 Answers

You can do this without the NodeJS crypto module, creating a package looked a bit like breaking a fly on the wheel to me so I figured out this:

if (Meteor.isServer) {
  Meteor.methods({
    'base64Encode':function(unencoded) {
      return new Buffer(unencoded || '').toString('base64');
    },
    'base64Decode':function(encoded) {
      return new Buffer(encoded || '', 'base64').toString('utf8');
    },
    'base64UrlEncode':function(unencoded) {
      var encoded = Meteor.call('base64Encode',unencoded);
      return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
    },
    'base64UrlDecode':function(encoded) {
      encoded = encoded.replace(/-/g, '+').replace(/_/g, '/');
      while (encoded.length % 4)
        encoded += '=';
      return Meteor.call('base64Decode',encoded);
    }
    console.log(Meteor.call('base64Encode','abc'));
});

This is based on the base64.js by John Hurliman found at https://gist.github.com/jhurliman/1250118 Note that this will work like a charm on the server but for porting it to the client you have call the methods with a callback function that stores the result as a session variable.

like image 96
Moritz Walter Avatar answered Dec 09 '25 23:12

Moritz Walter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!