Hi,

I recently needed to generate the dataset's hashed-id from a reporting script 
outside galaxy (a script that reads Galaxy database directly).

Here's how to generate the hashed-id from perl and postgres, hope it would save 
you some time if you ever need it.
(The python code to do the same is of course already in galaxy, see "encode_id" 
in galaxy/web/security/__init__.py).

=== Perl ===
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::Blowfish;

my $id = shift or die "Usage: $0 HISTORY-DATASET-ASSOCIATION-ID\n";
die "Invalid ID ($id)\n" unless $id =~ /^\d+$/;

## This is the same value of "id_secret" from your "universe_wsgi.ini" file
my $glx_id_secret = "My_Secret_Code" ;

my $cipher = new Crypt::Blowfish ( $glx_id_secret ); 
my $padded_str_id = ( '!' x (8- length($id) % 8 ) ). $id ;
my $ciphertext = $cipher->encrypt($padded_str_id);
my $hash_id = unpack('H*', $ciphertext ) ;

print $hash_id, "\n";
===============

=== PostgreqSQL ===
CREATE FUNCTION galaxy_encode_id(HISTORY_DATASET_ASSOCIATION_ID integer, 
GALAXY_ID_SECRET bytea) RETURNS text AS $$
SELECT substring(
  encode(
     encrypt( 
        decode(lpad('', 8-length($1)%8,'!')||$1, 'escape'), 
        $2,  'bf'),
  'hex') from 1 for 16 ) ;
$$ LANGUAGE SQL;
===========
(For postgreSQL, you'll need the "postgres-contrib" package, and run the 
"pgcrypto.sql" script).


This allows you to run a SQL command that automatically creates the preview 
URL, e.g.:

Show all datasets belonging to me that are bigger than 1GB, and add a "preview" 
URL link, so I can quickly check them out:
============
select 
  hda.name,
  history.name,
  dataset.file_size,
  'http://rave.cshl.edu/galaxy/datasets/' || galaxy_encode_id(hda.id, 
'my_secret_id') || '/display?preview=True' as PREVIEW_URL
from 
  history_dataset_association hda,
  galaxy_user,
  history,
  dataset
where
  history.user_id = galaxy_user.id
   AND
  hda.history_id = history.id
   AND
  hda.dataset_id = dataset.id
   AND
  hda.deleted = false
   AND
  galaxy_user.email ='gor...@cshl.edu'
   AND
  dataset.file_size > 1000000000
============


-gordon
___________________________________________________________
Please keep all replies on the list by using "reply all"
in your mail client.  To manage your subscriptions to this
and other Galaxy lists, please use the interface at:

  http://lists.bx.psu.edu/

Reply via email to