Skip to content

Commit

Permalink
Update signing examples to use sha512
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Nov 27, 2019
1 parent 580a386 commit 49ace3d
Show file tree
Hide file tree
Showing 19 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion assets/signing/sign-message.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ message <- enc2utf8(commandArgs(trailingOnly = TRUE))
key <- read_key(file = mykey, password = mypass)

# Create the signature
sig <- signature_create(serialize(message, NULL), key = key)
sig <- signature_create(serialize(message, NULL), hash = sha512, key = key) # Use hash = sha1 for QZ Tray 2.0 and older

print(sig)
2 changes: 1 addition & 1 deletion assets/signing/sign-message.asp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pk.LoadPemFile("private-key.pem")
key = pk.GetXml()
rsa.ImportPrivateKey(key)
rsa.EncodingMode = "base64"
sig = rsa.SignStringENC(data,"sha-1")
sig = rsa.SignStringENC(data,"SHA-512") ' Use "SHA-1" for QZ Tray 2.0 and older
Response.ContentType = "text/plain"
Response.Write Server.HTMLEncode(sig)
Expand Down
3 changes: 2 additions & 1 deletion assets/signing/sign-message.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
* @encoding I am the encoding used when returning the signature (base64 by default).
* @output false
*/
public any function sign(required string keyPath, required string message, string algorithm = "SHA1withRSA", string encoding = "base64") {
public any function sign(required string keyPath, required string message, string algorithm = "SHA512withRSA", string encoding = "base64") {
// Note: change algorithm to "SHA1withRSA" for QZ Tray 2.0 and older
createObject("java", "java.security.Security")
.addProvider(createObject("java", "org.bouncycastle.jce.provider.BouncyCastleProvider").init());
privateKey = createPrivateKey(fileRead(expandPath(keyPath)));
Expand Down
4 changes: 2 additions & 2 deletions assets/signing/sign-message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static string SignMessage(string request)
var cert = new X509Certificate2(KEY, PASS, STORAGE_FLAGS);
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PrivateKey;
byte[] data = new ASCIIEncoding().GetBytes(request);
byte[] hash = new SHA1CryptoServiceProvider().ComputeHash(data);
return Convert.ToBase64String(csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1")));
byte[] hash = new SHA512CryptoServiceProvider().ComputeHash(data); // Use SHA1CryptoServiceProvider for QZ Tray 2.0 and older
return Convert.ToBase64String(csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA512"))); // Use "SHA1" for QZ Tray 2.0 and older
}
catch(Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion assets/signing/sign-message.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func handler(w http.ResponseWriter, r *http.Request) {

hash := sha1.Sum([]byte(data))
rng := rand.Reader
signature, err := rsa.SignPKCS1v15(rng, rsaPrivateKey, crypto.SHA1, hash[:])
signature, err := rsa.SignPKCS1v15(rng, rsaPrivateKey, crypto.SHA512, hash[:]) // Use crypto.SHA1 for QZ Tray 2.0 and older
if err != nil {
displayError(w, "Error from signing: %s\n", err)
return
Expand Down
2 changes: 1 addition & 1 deletion assets/signing/sign-message.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public MessageSigner(byte[] keyData) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(parseKeyData(keyData));
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey key = kf.generatePrivate(keySpec);
sig = Signature.getInstance("SHA1withRSA");
sig = Signature.getInstance("SHA512withRSA"); // Use "SHA1withRSA" for QZ Tray 2.0 and older
sig.initSign(key);
}

Expand Down
3 changes: 2 additions & 1 deletion assets/signing/sign-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ var privateKey = "-----BEGIN PRIVATE KEY-----\n" +
"EjzSn7DcDE1tL2En/tSVXeUY\n" +
"-----END PRIVATE KEY-----";

qz.security.setSignatureAlgorithm("SHA512"); // Since 2.1
qz.security.setSignaturePromise(function(toSign) {
return function(resolve, reject) {
try {
var pk = KEYUTIL.getKey(privateKey);
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
var sig = new KJUR.crypto.Signature({"alg": "SHA512withRSA"}); // Use "SHA1withRSA" for QZ Tray 2.0 and older
sig.init(pk);
sig.updateString(toSign);
var hex = sig.sign();
Expand Down
2 changes: 1 addition & 1 deletion assets/signing/sign-message.jsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let request = "test data"
// openssl pkcs12 -export -in private-key.pem -inkey digital-certificate.txt -out private-key.pfx
let cert = new X509Certificate2("private-key.pfx")

let sha1 = new SHA1CryptoServiceProvider()
let sha1 = new SHA512CryptoServiceProvider() // Use "SHA1CryptoServiceProvider" for QZ Tray 2.0 and older

let csp = cert.PrivateKey :?> RSACryptoServiceProvider
let encoder = new ASCIIEncoding()
Expand Down
2 changes: 1 addition & 1 deletion assets/signing/sign-message.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private String getSignature(Object o) {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyData);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey key = kf.generatePrivate(keySpec);
Signature sig = Signature.getInstance("SHA1withRSA");
Signature sig = Signature.getInstance("SHA512withRSA"); // Use "SHA1withRSA" for QZ Tray 2.0 and older
sig.initSign(key);
sig.update(req.getBytes());
String output = DatatypeConverter.printBase64Binary(sig.sign());
Expand Down
2 changes: 1 addition & 1 deletion assets/signing/sign-message.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ app.get('/sign', function(req, res) {
var toSign = req.query.requestToSign;

fs.readFile(path.join(__dirname, '\\' + key), 'utf-8', function(err, privateKey) {
var sign = crypto.createSign('SHA1');
var sign = crypto.createSign('SHA512'); // Use "SHA1" for QZ Tray 2.0 and older

sign.update(toSign);
var signature = sign.sign({ key: privateKey/*, passphrase: pass */ }, 'base64');
Expand Down
2 changes: 1 addition & 1 deletion assets/signing/sign-message.odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ def index(self, **kwargs):
key_file.close()
password = None
pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key, password)
sign = crypto.sign(pkey, kwargs.get('request', ''), 'sha1')
sign = crypto.sign(pkey, kwargs.get('request', ''), 'sha512') # Use 'sha1' for QZ Tray 2.0 and older
data_base64 = base64.b64encode(sign)
return request.make_response(data_base64, [('Content-Type', 'text/plain')])
3 changes: 2 additions & 1 deletion assets/signing/sign-message.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
$privateKey = openssl_get_privatekey(file_get_contents($KEY) /*, $PASS */);

$signature = null;
openssl_sign($req, $signature, $privateKey);
openssl_sign($req, $signature, $privateKey, "sha512"); // Use "sha1" for QZ Tray 2.0 and older

/*
// Or alternately, via phpseclib
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa.setHash('sha512'); // Use 'sha1' for QZ Tray 2.0 and older
$rsa->loadKey(file_get_contents($KEY));
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
$signature = $rsa->sign($req);
Expand Down
2 changes: 1 addition & 1 deletion assets/signing/sign-message.pl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
my $rsa = Crypt::OpenSSL::RSA->new_private_key($private_key);

# Create signature
$rsa->use_sha1_hash();
$rsa->use_sha512_hash(); # use_sha1_hash for QZ Tray 2.0 and older
my $sig = encode_base64($rsa->sign($request));

print $sig;
2 changes: 1 addition & 1 deletion assets/signing/sign-message.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def index(request):
)

# Create the signature
signature = key.sign(message.encode('utf-8'), padding.PKCS1v15(), hashes.SHA1())
signature = key.sign(message.encode('utf-8'), padding.PKCS1v15(), hashes.SHA512()) # Use hashes.SHA1 for QZ Tray 2.0 and older

# Echo the signature
return HttpResponse(base64.b64encode(signature), content_type="text/plain")
Expand Down
2 changes: 1 addition & 1 deletion assets/signing/sign-message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Typical rails controller
class PrintingController < ActionController::Base
def sign
digest = OpenSSL::Digest.new('sha1')
digest = OpenSSL::Digest.new('sha512') # Use 'sha1' for QZ Tray 2.0 and older
pkey = OpenSSL::PKey::read(File.read(Rails.root.join('lib', 'certs', 'private-key.pem')))

signed = pkey.sign(digest, params[:request])
Expand Down
3 changes: 2 additions & 1 deletion assets/signing/sign-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ qz.security.setCertificatePromise((resolve, reject) => {
/*
* Client-side using jsrsasign
*/
qz.security.setSignatureAlgorithm("SHA512"); // Since 2.1
qz.security.setSignaturePromise(hash => {
return (resolve, reject) => {
fetch("assets/private-key.pem", {cache: 'no-store', headers: {'Content-Type': 'text/plain'}})
.then(wrapped => wrapped.text())
.then(data => {
var pk = KEYUTIL.getKey(data);
var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
var sig = new KJUR.crypto.Signature({"alg": "SHA512withRSA"}); // Use "SHA1withRSA" for QZ Tray 2.0 and older
sig.init(pk);
sig.updateString(hash);
var hex = sig.sign();
Expand Down
4 changes: 2 additions & 2 deletions assets/signing/sign-message.vb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ Public Sub SignMessage(message As String)
Dim csp As RSACryptoServiceProvider = CType(cert.PrivateKey,RSACryptoServiceProvider)

Dim data As Byte() = New ASCIIEncoding().GetBytes(message)
Dim hash As Byte() = New SHA1Managed().ComputeHash(data)
Dim hash As Byte() = New SHA512Managed().ComputeHash(data) ' Use SHA1Managed() for QZ Tray 2.0 and older

Response.ContentType = "text/plain"
Response.Write(Convert.ToBase64String(csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"))))
Response.Write(Convert.ToBase64String(csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA512")))) ' Use "SHA1" for QZ Tray 2.0 and older
Environment.[Exit](0)
End Sub
2 changes: 1 addition & 1 deletion assets/signing/sign_message.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ sign(Message, KeyPath) ->
{ok, Data} = file:read_file(KeyPath),
[KeyEntry] = public_key:pem_decode(Data),
PrivateKey = public_key:pem_entry_decode(KeyEntry),
Signature = public_key:sign(list_to_binary(Message), sha, PrivateKey),
Signature = public_key:sign(list_to_binary(Message), sha512, PrivateKey), % Use sha1 for QZ Tray 2.0 and older
Base64 = base64:encode(Signature),
io:fwrite(Base64).
1 change: 1 addition & 0 deletions sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,7 @@ <h4 class="panel-title">Options</h4>
"-----END CERTIFICATE-----\n");
});

qz.security.setSignatureAlgorithm("SHA512"); // Since 2.1
qz.security.setSignaturePromise(function(toSign) {
return function(resolve, reject) {
//Preferred method - from server
Expand Down

0 comments on commit 49ace3d

Please sign in to comment.